csharp full datetime code example

Example 1: c# date formats custom

// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}",      dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}",      dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}",      dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",          dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",               dt);  // "5 05"            minute
String.Format("{0:s ss}",               dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}",      dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}",      dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",               dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",           dt);  // "-6 -06 -06:00"   time zone

// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}",           dt);  // "3/9/2008"
String.Format("{0:MM/dd/yyyy}",         dt);  // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}",   dt);  // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}",           dt);  // "03/09/08"
String.Format("{0:MM/dd/yyyy}",         dt);  // "03/09/2008"

Example 2: c# datetime

// -------------------- DATE and TIME --------------------- //

// ------- DATETIME ------- //
// Create a DateTime object 
var year = 2020;
var month = 12;
var day = 3;

var date = new DateTime(year, month, day);


// Get the current date or time
var today = DateTime.Today;
var now = DateTime.Now;

Console.WriteLine(now.Hour);  // To get only the hour
Console.WriteLine(now.Minute);  // To get only the minutes


// How to modify DateTime objects?
var now = DateTime.Now;
var tomorrow = now.AddDay(1);
var monthBefore = now.AddMonths(-1);


// Convert DateTime to different string formats
var now = DateTime.Now;
string longDate = now.ToLongDateString();
string shortDate = now.ToShortDateString();
string normalDate = now.ToString("yyyy-MM-dd HH:mm");

// Convert string to DateTime
DateTime myDate = DateTime.Parse("2012-07-12");
//OR
DateTime myDate = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff",null);
 


// ------- TIMESPAN ------- //
// In this case we are working with duration or time intervals

// Create a TimeSpan object 
var hours = 2;
var minutes = 1;
var seconds = 7;

var timeSpan = new TimeSpan(hours, minutes, seconds);


// Get the timespan in hours, minutes, seconds,... 
var hourSpan = TimeSpan.FromHours(2);
var secondSpan = TimeSpan.FromSeconds(34);


// Convert DateTime to TimeSpan
var start = DateTime.Now;
var end = DateTime.Now.AddMinutes(90);

var duration = end - start; // The difference between 2 DateTimes returns a TimeSpan


// How to modify TimeSpan objects?
var duration = new TimeSpan(3, 6, 9);

var newDuration = duration.Add(TimeSpan.FromSeconds(26))       // Add 26 seconds
var newDuration = duration.Subtract(TimeSpan.FromSeconds(12))  // Subtract 12 seconds

  
// Convert a TimeSpan to a total of hours, minutes, seconds,... 
var timeSpan = new TimeSpan(2, 1, 0);
var totalMinutes = timeSpan.TotalMinutes; // Converts 2 hours and 1 minute to (60*2 + 1) minutes


// Convert a String to a TimeSpan
var duration = TimeSpan.Parse("04:07:12");
//OR
var duration = TimeSpan.ParseExact("23:59", @"hh\:mm", null) // This also verify if string is in that exact format of "hh:mm"