1. 程式人生 > >C#日期與時間

C#日期與時間

DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("yyyy-MM-dd"));  //按yyyy-MM-dd格式輸出s
Console.WriteLine(dt.ToString());    //  26/11/2009 AM 11:21:30
Console.WriteLine(dt.ToFileTime().ToString());   //   129036792908014024
// Converts the value of the current System.DateTime object to a Windows file time
Console.WriteLine(dt.ToFileTimeUtc().ToString());  //     129036792908014024
// Converts the value of the current System.DateTime object to a Windows file time
Console.WriteLine(dt.ToLocalTime().ToString());   //       26/11/2009 AM 11:21:30
// Converts the value of the current System.DateTime object to local time.
Console.WriteLine(dt.ToLongDateString().ToString());   //      2009年11月26日
Console.WriteLine(dt.ToLongTimeString().ToString());  //      AM 11:21:30
Console.WriteLine(dt.ToOADate().ToString());   //      40143.4732731597
Console.WriteLine(dt.ToShortDateString().ToString());   //     26/11/2009
Console.WriteLine(dt.ToShortTimeString().ToString());   //     AM 11:21
Console.WriteLine(dt.ToUniversalTime().ToString());   //       26/11/2009 AM 3:21:30
Console.WriteLine(dt.Year.ToString());   //        2009
Console.WriteLine(dt.Date.ToString());   //        26/11/2009 AM 12:00:00
Console.WriteLine(dt.DayOfWeek.ToString());  //       Thursday
Console.WriteLine(dt.DayOfYear.ToString());   //       330
Console.WriteLine(dt.Hour.ToString());       //        11
Console.WriteLine(dt.Millisecond.ToString());   //     801        (毫秒)
Console.WriteLine(dt.Minute.ToString());   //      21
Console.WriteLine(dt.Month.ToString());   //       11
Console.WriteLine(dt.Second.ToString());   //      30
Console.WriteLine(dt.Ticks.ToString());   //       633948312908014024
Console.WriteLine(dt.TimeOfDay.ToString());   //       12:29:51.5181524
// Gets the time of day for this instance.
// 返回 A System.TimeSpan that represents the fraction of the day that has elapsed since midnight.
Console.WriteLine(dt.ToString());     //     26/11/2009 PM 12:29:51
Console.WriteLine(dt.AddYears(1).ToString());    //         26/11/2010 PM 12:29:51
Console.WriteLine(dt.AddDays(1.1).ToString());    //        27/11/2009 PM 2:53:51
Console.WriteLine(dt.AddHours(1.1).ToString());    //       26/11/2009 PM 1:35:51
Console.WriteLine(dt.AddMilliseconds(1.1).ToString());    //26/11/2009 PM 12:29:51
Console.WriteLine(dt.AddMonths(1).ToString());    //        26/12/2009 PM 12:29:51
Console.WriteLine(dt.AddSeconds(1.1).ToString());    //     26/11/2009 PM 12:29:52
Console.WriteLine(dt.AddMinutes(1.1).ToString());    //     26/11/2009 PM 12:30:57
Console.WriteLine(dt.AddTicks(1000).ToString());    //      26/11/2009 PM 12:29:5
Console.WriteLine(dt.CompareTo(dt).ToString());    //       0
Console.WriteLine(dt.Add(new TimeSpan(1,0,0,0)).ToString());    // 加上一個時間段
//根據秒獲取當前的日期
public static DateTime GetDateTimeBySeconds(long seconds)
{ 
    return DateTime.Parse(DateTime.Now.ToString("1970-01-01 00:00:00")).AddSeconds(seconds);
}