C# 获取系统日期并拆分日、月、年

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12986114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:13:26  来源:igfitidea点击:

Get the system date and split day, month and year

c#asp.net

提问by Navanee Baskaran

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; }
}

回答by Peter

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

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

See DateTime Structuresin MSDN for additional references.

有关其他参考,请参阅MSDN 中的日期时间结构

回答by Ram

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..

在日期时间的地方,您可以使用您的专栏..

回答by Paul D'Ambra

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(string format)。如果您点击该链接,则有许多标准和自定义格式字符串的示例。

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.

将以您要求的格式将日期作为字符串提供。

回答by immayankmodi

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 yearhere.

或者,您可以在此处使用 split 函数将字符串日期拆分为日、月和年

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

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

回答by Aarthna Maheshwari

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;