获得系统日期和分裂日,月,年 [英] Get the system date and split day, month and year

查看:139
本文介绍了获得系统日期和分裂日,月,年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统日期格式为DD-MM-YYYY(20-10-2012),我得到的日期和使用隔板分割日期,月份和年份。我需要的日期格式(DD / MM / YYYY)转换是否在任何日期格式的格式的回报。

 字符串SDATE =的String.Empty;
日期时间_date = DateTime.Now;
诠释计数= 0;
字符串格式=DD-MM-YYYY;
SDATE = dateFormat.ToString(格式);
字符串[] =字sDate.Split(新的char [] {' - '});
的foreach(字串字)
{
    数+ = 1;
    如果(计数== 1){天=字; }
    如果(计数== 2){月=字; }
    如果(计数== 3){年=字; }
}


解决方案

您应该使用 DateTime.TryParseExcact 如果你知道的格式,如果没有,想使用系统设置 DateTime.TryParse 。和打印日期,则DateTime.ToString 与参数正确的格式。要获得年份,月份或一天你有 DateTime.Year DateTime.Month DateTime.Day

请参阅的DateTime结构在MSDN额外引用。

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.

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

解决方案

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.

See DateTime Structures in MSDN for additional references.

这篇关于获得系统日期和分裂日,月,年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆