csharp DateTime format
DateTime today = DateTime.Now; //date parts: Console.WriteLine(today.ToString()); //2010-10-12 15:13:58 Console.WriteLine(today.Day.ToString()); //12 Console.WriteLine(today.DayOfWeek.ToString()); //Tuesday Console.WriteLine(today.DayOfYear.ToString()); //285 Console.WriteLine(today.Hour.ToString()); //15 Console.WriteLine(today.Millisecond.ToString()); //330 Console.WriteLine(today.Minute.ToString()); //13 Console.WriteLine(today.Month.ToString()); //10 Console.WriteLine(today.Second.ToString()); //58 Console.WriteLine(today.Ticks.ToString()); //634224932383306527 Console.WriteLine(today.TimeOfDay.ToString()); //15:13:58.3306527 Console.WriteLine(today.TimeOfDay.Days.ToString()); //0 Console.WriteLine(today.TimeOfDay.Hours.ToString()); //15 Console.WriteLine(today.TimeOfDay.Milliseconds.ToString()); //330 Console.WriteLine(today.TimeOfDay.Minutes.ToString()); //13 Console.WriteLine(today.TimeOfDay.Seconds.ToString()); //58 Console.WriteLine(today.TimeOfDay.Ticks.ToString()); //548383306527 Console.WriteLine(today.TimeOfDay.TotalDays.ToString()); //0.634702901072917 Console.WriteLine(today.TimeOfDay.TotalHours.ToString()); //15.23286962575 Console.WriteLine(today.TimeOfDay.TotalMilliseconds.ToString()); //54838330.6527 Console.WriteLine(today.TimeOfDay.TotalMinutes.ToString()); //913.972177545 Console.WriteLine(today.TimeOfDay.TotalSeconds.ToString()); //54838.3306527 Console.WriteLine(today.Year.ToString()); //2010 //string formatting: Console.WriteLine(today.ToString("d")); //2010-10-12 Console.WriteLine(today.ToString("dd")); //12 Console.WriteLine(today.ToString("ddd")); //Tue Console.WriteLine(today.ToString("dddd")); //Tuesday Console.WriteLine(today.ToString("f")); //12 October 2010 15:27 Console.WriteLine(today.ToString("ff")); //94 Console.WriteLine(today.ToString("fff")); //941 Console.WriteLine(today.ToString("F")); //12 October 2010 15:27:17 Console.WriteLine(today.ToString("FF")); //94 Console.WriteLine(today.ToString("FFF")); //941 Console.WriteLine(today.ToString("g")); //2010-10-12 15:27 Console.WriteLine(today.ToString("gg")); //A.D. Console.WriteLine(today.ToString("hh")); //03 Console.WriteLine(today.ToString("HH")); //15 Console.WriteLine(today.ToString("m")); //12 October Console.WriteLine(today.ToString("mm")); //27 Console.WriteLine(today.ToString("M")); //12 October Console.WriteLine(today.ToString("MM")); //10 Console.WriteLine(today.ToString("MMM")); //Oct Console.WriteLine(today.ToString("MMMM")); //October Console.WriteLine(today.ToString("s")); //2010-10-12T15:27:17 Console.WriteLine(today.ToString("ss")); //17 Console.WriteLine(today.ToString("t")); //15:27 Console.WriteLine(today.ToString("tt")); //PM Console.WriteLine(today.ToString("y")); //October 2010 Console.WriteLine(today.ToString("yy")); //10 Console.WriteLine(today.ToString("yyy")); //2010 Console.WriteLine(today.ToString("yyyy")); //2010 Console.WriteLine(today.ToString("yyyyy")); //02010 Console.WriteLine(today.ToString("zz")); //+01 Console.WriteLine(today.ToString("zzz")); //+01:00 Console.WriteLine(today.ToString("yyyy:mm")); //2010:27 Console.WriteLine(today.ToString("yyyy/mm")); //2010-27 //adding: Console.WriteLine(today.AddDays(1)); //2010-10-13 15:36:31 Console.WriteLine(today.AddHours(1)); //2010-10-12 16:36:31 Console.WriteLine(today.AddMilliseconds(1)); //2010-10-12 15:36:31 Console.WriteLine(today.AddMinutes(1)); //2010-10-12 15:37:31 Console.WriteLine(today.AddMonths(1)); //2010-11-12 15:36:31 Console.WriteLine(today.AddSeconds(1)); //2010-10-12 15:36:32 Console.WriteLine(today.AddTicks(1000)); //2010-10-12 15:36:31 Console.WriteLine(today.AddYears(1)); //2011-10-12 15:36:31 //subtracting: Console.WriteLine(today.AddDays(-1)); //2010-10-11 15:37:45 Console.WriteLine(today.AddHours(-1)); //2010-10-12 14:37:45 Console.WriteLine(today.AddMilliseconds(-1)); //2010-10-12 15:37:45 Console.WriteLine(today.AddMinutes(-1)); //2010-10-12 15:36:45 Console.WriteLine(today.AddMonths(-1)); //2010-09-12 15:37:45 Console.WriteLine(today.AddSeconds(-1)); //2010-10-12 15:37:44 Console.WriteLine(today.AddTicks(-1000)); //2010-10-12 15:37:45 Console.WriteLine(today.AddYears(-1)); //2009-10-12 15:37:45 DateTime lastYear = today.AddYears(-1); Console.WriteLine((today.Subtract(lastYear)).Days.ToString()); //365 //comparison: if (today.ToString("yyyy-MM-dd HH") == "2010-10-12 12") Console.WriteLine("Match"); //split across lines for readability: Console.WriteLine( "Year: {0}, Month: {1}, Day: {2}", today.Year.ToString(), today.Month.ToString(), today.Day.ToString() ); //Year: 2010, Month: 10, Day: 12 //midnight: DateTime midnight = DateTime.Today; Console.WriteLine(midnight.Hour); //for favoured YYYY-MM-DD format, use: today.ToString("yyyy-MM-dd")
Example use of DateTime. See MSDN for the full reference.
Updated: Wednesday 20th April 2011, 07:10am
There are 0 comments
Comments are currently closed.