Getting date, hour, minute, am/pm from DateTime value in C#

Posted by Raja under C# category on | Views : 22959
Below code snippet shows how to get different values of DateTime field in C#

DateTime productDate = DateTime.Now;
// write only date
txtProductDate.Text = productDate.ToShortDateString();
// set the value in 12 hour format
string twelveHourFormatHour = int.Parse(productDate.ToString("hh")).ToString();
// set the value in 24 hour format
string twentyFourHourFormatHour = int.Parse(productDate.ToString("HH")).ToString();
// set the minute
string minutes = productDate.ToString("mm");
// get the AM and PM
string ampm = productDate.ToString("tt");


Comments or Responses

Login to post response