Date Time formatting and conversions in ASP.NET

Sometimes we have to do some calculations with date time objects in our applications. In that case we have to use Date Time formatting and conversions to accomplish the tasks.

In this example we can see some commonly used methods to do the conversions and manipulations.

This covers the formatting including :

  • Convert string into date time.
  • Convert UTC/GMT into local time.
  • Format date time using cultures.
  • Use Short and Long forms of date times.

Click on the image to Zoom

date time formattingI just simply put the source code of the above table I used in a Razor view(.cshtml file)

Code for the above Date Time formatting page

@using System.Globalization
@{
ViewBag.Title = "DateTimeFormats";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Date Time Formats</h2>
<h3>Current Time Zone : @TimeZone.CurrentTimeZone.StandardName : @DateTimeOffset.Now.Offset.ToString()</h3>
<h3>Current Time of server : @DateTime.Now.ToString() </h3>
<table class="table table-bordered">
<thead>
<tr>
<th>Function</th>
<th>Original Value</th>
<th>Converted Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<!-- Current Date and Time of Server -->
<td>DateTime.Now</td>
<td>@DateTime.Now.ToString()</td>
<td>@DateTime.Now.ToString()</td>
<td>Current Date and time of the server</td>
</tr>
<tr>
<!-- Current Date and Time of Server in UTC format -->
<td>DateTime.UtcNow</td>
<td>@DateTime.Now.ToString()</td>
<td>@DateTime.UtcNow.ToString()</td>
<td>Current Date and time of the server expressed as UTC</td>
</tr>
<tr>
<td>DateTime ToLocalTime()</td>
@{
DateTime temp = new DateTime(2016, 12, 31, 23, 32, 30);
<td>@temp.ToString()</td>
<td>@temp.ToLocalTime().ToString()</td>
}
<td>Converts the specified DateTime to local time considering the Time Zone</td>
</tr>
<tr>
<td>DateTime ToShortDateString()</td>
<td>@DateTime.Now.ToString()</td>
<td>@DateTime.Now.ToShortDateString()</td>
<td>Get only the Date part</td>
</tr>
<tr>
<td>DateTime ToShortTimeString()</td>
<td>@DateTime.Now.ToString()</td>
<td>@DateTime.Now.ToShortTimeString()</td>
<td>Get only the Time part HH:MM AM/PM</td>
</tr>
<tr>
<td>DateTime ToLongDateString()</td>
<td>@DateTime.Now.ToString()</td>
<td>@DateTime.Now.ToLongDateString()</td>
<td>Get the long date</td>
</tr>
<tr>
<td>DateTime Formatting with culture</td>
@{
<td>@DateTime.Now.ToString()</td>
<td>
<div>@DateTime.Now.ToString(new CultureInfo("de-DE"))</div>
<div>@DateTime.Now.ToString(new CultureInfo("nl-NL"))</div>
</td>
}

<td>
<div>Format the date with specific culture (Ex: de-DE)</div>
<div>Format the date with specific culture (Ex: nl-NL)</div>
</td>
</tr>
<tr>
<td>Formatting GMT/UTC time with culture</td>
@{
CultureInfo ci = new CultureInfo("de-DE");
var GMTTime = DateTimeOffset.Parse("2017-03-30T11:01:32Z ");
<td>"2017-03-30T11:01:32Z"</td>
<td>
<div>@DateTime.Parse("2017-03-30T11:01:32Z ").ToString()</div>

<div>@DateTime.Parse("2017-03-30T11:01:32Z ").ToString(new CultureInfo("eu-ES"))</div>
<div>@GMTTime.ToString("MM/dd/yyyy hh:mm:sszzz", ci)</div>
</td>
}

<td>
<div>Format GMT/UTC time to local time</div>
<div>Format GMT/UTC time to "eu-ES" culture</div>
<div>Format GMT/UTC time to "de-DE" culture without adding local offset</div>
</td>
</tr>
<tr>
<td>Add offset to UTC</td>
<td>"2017-03-30T11:01:32Z"</td>
@{
GMTTime = DateTimeOffset.Parse("2017-03-30T11:01:32Z");
<td>@GMTTime.DateTime.AddMinutes(390)</td>
}
<td>Adding offset (+6:30 hours) to the GMT/UTC time</td>
</tr>
</tbody>
</table>

Please note that this article only based on commonly used formattings and there are more formattings available in .NET.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.