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 spa
//本年度,用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(); code
//本季度,不少人都會以爲這裏難點,須要寫個長長的過程來判斷。其實不用的,咱們都知道一年四個季度,一個季度三個月
//首先咱們先把日期推到本季度第一個月,而後這個月的第一天就是本季度的第一天了
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(); orm
//如何得到當月有多少天 get
int m=System.DateTime.DaysInMonth(System.DateTime.Now.Year,System.DateTime.Now.Month); string
//獲取某月的實際工做日(即不包括週六日) it
//調用
//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; io
}
}
return mm;
} table
得到任意兩日期之間的有效工做日(不包括週六日) 得到任意兩日期之間的有效工做日(不包括週六日)#region 得到任意兩日期之間的有效工做日(不包括週六日) ast
//調用
//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;