获取系统日期并分割日,月和年

时间:2022-09-27 20:23:10

My system date format is dd-MM-yyyy(20-10-2012) and I'm getting the date and using separator to split the date, month and year. I need to convert date format (dd/MM/yyyy) whether the formats returns at any date format.

我的系统日期格式是dd-MM-yyyy(20-10-2012),我得到日期并使用分隔符来分割日期,月份和年份。我需要转换日期格式(dd / MM / yyyy)格式是否以任何日期格式返回。

string sDate = string.Empty;
DateTime _date = DateTime.Now;
int count = 0;
string format = "dd-MM-yyyy";
sDate = dateFormat.ToString(format);
string[] Words = sDate.Split(new char[] { '-' });
foreach (string Word in Words)
{
    count += 1;
    if (count == 1) { Day = Word; }
    if (count == 2) { Month = Word; }
    if (count == 3) { Year = Word; }
}

5 个解决方案

#1


16  

Here is what you are looking for:

这是你在找什么:

String sDate = DateTime.Now.ToString();
DateTime datevalue = (Convert.ToDateTime(sDate.ToString()));

String dy = datevalue.Day.ToString();
String mn = datevalue.Month.ToString();
String yy = datevalue.Year.ToString();

OR

Alternatively, you can use split function to split string date into day, month and year here.

或者,您可以使用拆分功能将字符串日期分为日,月和年。

Hope, it will helps you... Cheers. !!

希望,它会帮助你...干杯。 !

#2


12  

You can do like follow:

您可以这样做:

 String date = DateTime.Now.Date.ToString();
    String Month = DateTime.Now.Month.ToString();
    String Year = DateTime.Now.Year.ToString();

On the place of datetime you can use your column..

在日期时间你可以使用你的专栏..

#3


6  

You should use DateTime.TryParseExcact if you know the format, or if not and want to use the system settings DateTime.TryParse. And to print the date,DateTime.ToString with the right format in the argument. To get the year, month or day you have DateTime.Year, DateTime.Month or DateTime.Day.

如果您知道格式,则应使用DateTime.TryParseExcact,如果不知道,则应使用系统设置DateTime.TryParse。并在参数中以正确的格式打印日期DateTime.ToString。要获得年,月或日,您有DateTime.Year,DateTime.Month或DateTime.Day。

See DateTime Structures in MSDN for additional references.

有关其他参考,请参阅MSDN中的DateTime Structures。

#4


4  

You can split date month year from current date as follows:

您可以从当前日期分割日期月份,如下所示:

DateTime todaysDate = DateTime.Now.Date;

DateTime todaysDate = DateTime.Now.Date;

Day:

int day = todaysDate.Day;

int day = todaysDate.Day;

Month:

int month = todaysDate.Month;

int month = todaysDate.Month;

Year:

int year = todaysDate.Year;

int year = todaysDate.Year;

#5


1  

Without opening an IDE to check my brain works properly for syntax at this time of day...

没有打开IDE来检查我的大脑在一天的这个时候正确地运行语法...

If you simply want the date in a particular format you can use DateTime's .ToString(string format). There are a number of examples of standard and custom formatting strings if you follow that link.

如果您只想以特定格式使用日期,则可以使用DateTime的.ToString(字符串格式)。如果您遵循该链接,则有许多标准和自定义格式字符串示例。

So

DateTime _date = DateTime.Now;
var _dateString = _date.ToString("dd/MM/yyyy");

would give you the date as a string in the format you request.

会以您请求的格式将日期作为字符串给出。

#1


16  

Here is what you are looking for:

这是你在找什么:

String sDate = DateTime.Now.ToString();
DateTime datevalue = (Convert.ToDateTime(sDate.ToString()));

String dy = datevalue.Day.ToString();
String mn = datevalue.Month.ToString();
String yy = datevalue.Year.ToString();

OR

Alternatively, you can use split function to split string date into day, month and year here.

或者,您可以使用拆分功能将字符串日期分为日,月和年。

Hope, it will helps you... Cheers. !!

希望,它会帮助你...干杯。 !

#2


12  

You can do like follow:

您可以这样做:

 String date = DateTime.Now.Date.ToString();
    String Month = DateTime.Now.Month.ToString();
    String Year = DateTime.Now.Year.ToString();

On the place of datetime you can use your column..

在日期时间你可以使用你的专栏..

#3


6  

You should use DateTime.TryParseExcact if you know the format, or if not and want to use the system settings DateTime.TryParse. And to print the date,DateTime.ToString with the right format in the argument. To get the year, month or day you have DateTime.Year, DateTime.Month or DateTime.Day.

如果您知道格式,则应使用DateTime.TryParseExcact,如果不知道,则应使用系统设置DateTime.TryParse。并在参数中以正确的格式打印日期DateTime.ToString。要获得年,月或日,您有DateTime.Year,DateTime.Month或DateTime.Day。

See DateTime Structures in MSDN for additional references.

有关其他参考,请参阅MSDN中的DateTime Structures。

#4


4  

You can split date month year from current date as follows:

您可以从当前日期分割日期月份,如下所示:

DateTime todaysDate = DateTime.Now.Date;

DateTime todaysDate = DateTime.Now.Date;

Day:

int day = todaysDate.Day;

int day = todaysDate.Day;

Month:

int month = todaysDate.Month;

int month = todaysDate.Month;

Year:

int year = todaysDate.Year;

int year = todaysDate.Year;

#5


1  

Without opening an IDE to check my brain works properly for syntax at this time of day...

没有打开IDE来检查我的大脑在一天的这个时候正确地运行语法...

If you simply want the date in a particular format you can use DateTime's .ToString(string format). There are a number of examples of standard and custom formatting strings if you follow that link.

如果您只想以特定格式使用日期,则可以使用DateTime的.ToString(字符串格式)。如果您遵循该链接,则有许多标准和自定义格式字符串示例。

So

DateTime _date = DateTime.Now;
var _dateString = _date.ToString("dd/MM/yyyy");

would give you the date as a string in the format you request.

会以您请求的格式将日期作为字符串给出。