【转】【Unity】DateTime各种时间字符串各种表⽰时间的⽅法谢谢⽹络上的⼤神此⽚为转载的⽂章
DateTime.Now.ToShortTimeString()
DateTime dt = DateTime.Now;
dt.ToString();//2005-11-5 13:21:25
dt.ToFileTime().ToString();//127756416859912816
dt.ToFileTimeUtc().ToString();//127756704859912816
dt.ToLocalTime().ToString();//2005-11-5 21:21:25
dt.ToLongDateString().ToString();//2005年11⽉5⽇
dt.ToLongTimeString().ToString();//13:21:25
dt.ToOADate().ToString();//38661.5565508218
dt.ToShortDateString().ToString();//2005-11-5
dt.ToShortTimeString().ToString();//13:21
dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
dt.Year.ToString();//2005
dt.Date.ToString();//2005-11-5 0:00:00
dt.DayOfWeek.ToString();//Saturday
dt.DayOfYear.ToString();//309
dt.Hour.ToString();//13
dt.Millisecond.ToString();//441
dt.Minute.ToString();//30
dt.Month.ToString();//11
dt.Second.ToString();//28
dt.Ticks.ToString();//632667942284412864
dt.TimeOfDay.ToString();//13:30:28.4412864
dt.ToString();//2005-11-5 13:47:04
dt.AddYears(1).ToString();//2006-11-5 13:47:04
字符串截取日期dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
dt.AddMonths(1).ToString();//2005-12-5 13:47:04
dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
dt.CompareTo(dt).ToString();//0
dt.Add(?).ToString();//问号为⼀个时间段
dt.Equals(“2005-11-6 16:11:04”).ToString();//False
dt.Equals(dt).ToString();//True
dt.GetHashCode().ToString();//1474088234
dt.GetType().ToString();//System.DateTime
dt.GetTypeCode().ToString();//DateTime
dt.GetDateTimeFormats(’s’)[0].ToString();//2005-11-05T14:06:25
dt.GetDateTimeFormats(‘t’)[0].ToString();//14:06
dt.GetDateTimeFormats(‘y’)[0].ToString();//2005年11⽉
dt.GetDateTimeFormats(‘D’)[0].ToString();//2005年11⽉5⽇
dt.GetDateTimeFormats(‘D’)[1].ToString();//2005 11 05
dt.GetDateTimeFormats(‘D’)[2].ToString();//星期六 2005 11 05
dt.GetDateTimeFormats(‘D’)[3].ToString();//星期六 2005年11⽉5⽇
dt.GetDateTimeFormats(‘M’)[0].ToString();//11⽉5⽇
dt.GetDateTimeFormats(‘f’)[0].ToString();//2005年11⽉5⽇ 14:06
dt.GetDateTimeFormats(‘g’)[0].ToString();//2005-11-5 14:06
dt.GetDateTimeFormats(‘r’)[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
string.Format(“{0:d}”,dt);//2005-11-5
string.Format(“{0}”,dt);//2005年11⽉5⽇
string.Format(“{0:f}”,dt);//2005年11⽉5⽇ 14:23
string.Format(“{0:F}”,dt);//2005年11⽉5⽇ 14:23:23
string.Format(“{0:g}”,dt);//2005-11-5 14:23
string.Format(“{0:G}”,dt);//2005-11-5 14:23:23
string.Format(“{0:M}”,dt);//11⽉5⽇
string.Format(“{0:R}”,dt);//Sat, 05 Nov 2005 14:23:23 GMT
string.Format(“{0:s}”,dt);//2005-11-05T14:23:23
string.Format(“{0:t}”,dt);//14:23
string.Format(“{0:T}”,dt);//14:23:23
string.Format(“{0:u}”,dt);//2005-11-05 14:23:23Z
string.Format(“{0:U}”,dt);//2005年11⽉5⽇ 6:23:23
string.Format(“{0:Y}”,dt);//2005年11⽉
string.Format(“{0}”,dt);//2005-11-5 14:23:23
string.Format(“{0:yyyyMMddHHmmssffff}”,dt);
计算2个⽇期之间的天数差
DateTime dt1 = Convert.DateTime(“2007-8-1”);
DateTime dt2 = Convert.DateTime(“2007-8-15”);
TimeSpan span = dt2.Subtract(dt1);
int dayDiff = span.Days + 1;
计算某年某⽉的天数
int days = DateTime.DaysInMonth(2007, 8);
days = 31;
给⽇期增加⼀天、减少⼀天
DateTime dt =DateTime.Now;
dt.AddDays(1); //增加⼀天
dt.AddDays(-1);//减少⼀天
其它年份⽅法类似…
Oracle SQL⾥转换⽇期函数
to_date(“2007-6-6”,’YYYY-MM-DD”);
to_date(“2007/6/6”,’yyyy/mm/dd”);
如下⼀组数据,如何查表⾥包含9⽉份的记录:
CGGC_STRATDATE CGGC_ENDDATE
2007-8-4 2007-9-5
2007-9-5 2007-9-20
2007-9-22 2007-10-5
SELECT * FROM TABLE
(TO_DATE(‘2007/9/1’,’yyyy/mm/dd’) BETWEEN CGGC_STRATDATE
AND CGGC_ENDDATE OR CGGC_STRATDATE >=TO_DATE(‘2007/9/1’,’yyyy/mm/dd’)
AND CGGC_ENDDATE<=TO_DATE(‘2007/9/30’,’yyyy/mm/dd’) ”
OR TO_DATE(‘2007/9/30’,’yyyy/mm/dd’) BETWEEN CGGC_STRATDATE
AND CGGC_ENDDATE) ORDER BY CGGC_STRATDATE ASC
//本年度,⽤ToString的字符格式化我们也很容易地算出本年度的第⼀天和最后⼀天
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddYears(1).AddDays(-1).ToShortDateString();
//上年度,不⽤再解释了吧
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddYears(-1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddDays(-1).ToShortDateString();
//下年度
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddYears(1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddYears(2).AddDays(-1).ToShortDateString();
//本季度,很多⼈都会觉得这⾥难点,需要写个长长的过程来判断。其实不⽤的,我们都知道⼀年四个季度,⼀个季度三个⽉//⾸先我们先把⽇期推到本季度第⼀个⽉,然后这个⽉的第⼀天就是本季度的第⼀天了
DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”);
//同理,本季度的最后⼀天就是下季度的第⼀天减⼀
DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”)).AddDays(-1).ToShortDateString();
//下季度
DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”);
DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”)).AddDays(-1).ToShortDateString();
//上季度
DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”);
DateTime.Parse(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”)).AddDays(-
1).ToShortDateString();
//如何获得当⽉有多少天
int m=System.DateTime.DaysInMonth(System.DateTime.Now.Year,System.DateTime.Now.Month);
//获取某⽉的实际⼯作⽇(即不包括周六⽇)
//调⽤
//int days =getDays(System.DateTime.Now));
private int getDays(System.DateTime date1)
{
int m=System.DateTime.DaysInMonth(date1.Year,date1.Month);
int mm=0;
for(int i=1;i<=m;i++)
{
System.DateTime date=Convert.ToDateTime(date1.Year+”-“+date1.Month+”-“+i);
switch (date.DayOfWeek)
{
case System.DayOfWeek.Monday:
case System.DayOfWeek.Thursday:
case System.DayOfWeek.Tuesday:
case System.DayOfWeek.Wednesday:
case System.DayOfWeek.Friday:
mm=mm+1;
break;
}
}
return mm;
}
获得任意两⽇期之间的有效⼯作⽇(不包括周六⽇) 获得任意两⽇期之间的有效⼯作⽇(不包括周六⽇)#region 获得任意两⽇期之间的有效⼯作⽇(不包括周六⽇)
//调⽤
//DateTime date1=Convert.ToDateTime(“2005-10-20”);
//DateTime date2=Convert.ToDateTime(“2005-11-01”);
//int days =getDays(date1,date2);
private int getDays(System.DateTime date1,System.DateTime date2)
{
string m=DateDiff(EnumDateCompare.day,date1,date2).ToString(“f0”);
int mm=0;
for(int i=0;i<=Convert.ToInt32(m);i++)
{
System.DateTime date=Convert.ToDateTime(date1.AddDays(i));
switch (date.DayOfWeek)
{
case System.DayOfWeek.Monday:
case System.DayOfWeek.Thursday:
case System.DayOfWeek.Tuesday:
case System.DayOfWeek.Wednesday:
case System.DayOfWeek.Friday:
mm=mm+1;
break;
}
}
return mm;
}
#endregion
格式输出
格式输出#region 格式输出
private void Page_Load(object sender, System.EventArgs e)
{
System.Globalization.DateTimeFormatInfo myDTFI = new System.Globalization.CultureInfo( “en-US”, false ).DateTimeFormat;
//中国⽤zh-cn
DateTime myDT =System.DateTime.Now;
Response.Write(myDT.ToString(“f”,myDTFI));
/////*//
This code produces the following output.
FORMAT en-US EXAMPLE
CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY
d 1/3/2002 M/d/yyyy (ShortDatePattern)
D Thursday, January 03, 2002 dddd, MMMM dd, yyyy (LongDatePattern)
f Thursday, January 03, 2002 12:00 AM
F Thursday, January 03, 2002 12:00:00 AM dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern) g 1/3/2002 12:00 AM
G 1/3/2002 12:00:00 AM
m January 03 MMMM dd (MonthDayPattern)
M January 03 MMMM dd (MonthDayPattern)
r Thu, 03 Jan 2002 00:00:00 GMT ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ (RFC1123Pattern)
R Thu, 03 Jan 2002 00:00:00 GMT ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ (RFC1123Pattern)
s 2002-01-03T00:00:00 yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss (SortableDateTimePattern)
t 12:00 AM h:mm tt (ShortTimePattern)
T 12:00:00 AM h:mm:ss tt (LongTimePattern)
u 2002-01-03 00:00:00Z yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ (UniversalSortableDateTimePattern)
U Thursday, January 03, 2002 8:00:00 AM
y January, 2002 MMMM, yyyy (YearMonthPattern)
Y January, 2002 MMMM, yyyy (YearMonthPattern)
*/
}
endregion
获得本周的周六和周⽇
ConvertDateToWeek#region ConvertDateToWeek
public static void ConvertDateToWeek(DateTime date,out DateTime firstdate,out DateTime lastdate) {
DateTime first=System.DateTime.Now;
DateTime last=System.DateTime.Now;
switch (date.DayOfWeek)
{ case System.DayOfWeek.Monday:
first=date.AddDays(-1);
last=date.AddDays(5);
break;
case System.DayOfWeek.Tuesday:
first=date.AddDays(-2);
last=date.AddDays(4);
break;
case System.DayOfWeek.Wednesday:
first=date.AddDays(-3);
last=date.AddDays(3);
break;
case System.DayOfWeek.Thursday:
first=date.AddDays(-4);
last=date.AddDays(2);
break;
case System.DayOfWeek.Friday:
first=date.AddDays(-5);
last=date.AddDays(1);
break;
case System.DayOfWeek.Saturday:
first=date.AddDays(-6);
last=date;
break;
case System.DayOfWeek.Sunday:
first=date;
last=date.AddDays(6);
break;
}
firstdate=first;
lastdate=last;
}
endregion
//调⽤
DateTime firstdate=System.DateTime.Now;
DateTime lastdate=System.DateTime.Now;
ConvertDateToWeek(date,out firstdate,out lastdate);
获得当前⽇期是该年度的第⼏周
DateTime dt = Convert.ToDateTime(“2006-05-01”); int weeks = dt.DayOfYear / 7 + 1;
原⽂链接

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。