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

我的系统日期格式是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; } } 

这是你在找什么:

 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(); 

要么

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

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

您可以这样做:

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

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

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

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

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

DateTime todaysDate = DateTime.Now.Date;

天:

int day = todaysDate.Day;

月:

int month = todaysDate.Month;

年:

int year = todaysDate.Year;

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

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

所以

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

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