Date Time Formats Used in C# MVC

Introduction

Many times while working with an application, everyone has to insert Date, DateTime, or Custom formatted date into the database, or we have to show the date on UI as well. Sometimes, we have to get the Date and Time of the client machine. We will learn some Date formats used in MVC.

Step 1: Create one Controller with the Action name and return type of this action in View, as shown below:
  1. public class DemoController : Controller    
  2. {    
  3.     public ActionResult DtLocation()    
  4.     {    
  5.         // return 1/1/0001 12:00:00 AM  
  6.         DateTime defaultDate = default(DateTime);  
  7.         // return 08/05/2016 12:56 PM  
  8.         var shortDT = defaultDate.ToString().Replace("12:00:00 AM""");   
  9.         // return 08/05/2016 12:56 PM     
  10.         var userDt = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");  
  11.         // return 08/05/2016 12:56 PM  
  12.         var nwDt = DateTime.Now.ToShortDateString();  
  13.         // return 12:24 PM  
  14.         var nwTm = DateTime.Now.ToShortTimeString();  
  15.         // return 8/5/2016 12:00:00 AM  
  16.         DateTime dtByUser = DateTime.Parse(userDt).Date;  
  17.         // return Friday, August 05, 2016  
  18.         var longDt = dtByUser.ToLongDateString();  
  19.         // return 12:00:00 AM  
  20.         var shortTm = dtByUser.ToLongTimeString();  
  21.         // return 2016-08-05  
  22.         var formattedDt = dtByUser.ToString("yyyy-MM-dd");  
  23.         // return Friday, 05 August 2016    
  24.         var fDt = DateTime.Now.ToString("dddd, dd MMMM yyyy");  
  25.         ViewData["removeTm"] = shortDT;    
  26.         ViewData["nowDt"] = nwDt;    
  27.         ViewData["nowTm"] = nwTm;    
  28.         ViewData["userDt"] = dtByUser;    
  29.         ViewData["longDt"] = longDt;    
  30.         ViewData["shortTm"] = shortTm;    
  31.         ViewData["formattedDt"] = formattedDt;    
  32.         ViewData["fDt"] = fDt;    
  33.         TimeZone zone = TimeZone.CurrentTimeZone;    
  34.         ViewData["CurrentTimeZone"] = zone.StandardName;  
  35.         // return 05:30:00  
  36.         TimeSpan UTCOffset = zone.GetUtcOffset(DateTime.Now);  
  37.         ViewData["UTCOffset"] = UTCOffset;    
  38.         System.Globalization.DaylightTime dayLightTm = zone.GetDaylightChanges(dtByUser.Year);    
  39.         ViewData["dayLightStartTm"] = dayLightTm.Start.ToString("hh:mm:ss tt");  
  40.         // return 8/5/2016 12:56:18 PM  
  41.         var s = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, zone.StandardName);    
  42.         ViewData["ConvertedTZone"] = s;    
  43.         return View();    
  44.     }    
  45. } 
Step 2: Now, create one View for the above action and call all ViewData, which will show the date format results in UI.
  1. @{    
  2.     Layout = null;    
  3. }    
  4. <div style="width: 100%;">    
  5.     <table style="width: 100%;">    
  6.         <tr>    
  7.             <td style="width: 15%;">    
  8.                 Remove Time From Default Date :    
  9.             </td>    
  10.             <td style="width: 50%;">    
  11.                 @ViewData["removeTm"]    
  12.             </td>    
  13.         </tr>    
  14.         <tr>    
  15.             <td style="width: 15%;">    
  16.                 Todays Date :    
  17.             </td>    
  18.             <td style="width: 50%;">    
  19.                 @ViewData["nowDt"]    
  20.             </td>    
  21.         </tr>    
  22.         <tr>    
  23.             <td style="width: 15%;">    
  24.                 Current Time :    
  25.             </td>    
  26.             <td style="width: 50%;">    
  27.                 @ViewData["nowTm"]    
  28.             </td>    
  29.         </tr>    
  30.         <tr>    
  31.             <td style="width: 15%;">    
  32.                 User Date :    
  33.             </td>    
  34.             <td style="width: 50%;">    
  35.                 @ViewData["userDt"]    
  36.             </td>    
  37.         </tr>    
  38.         <tr>    
  39.             <td style="width: 15%;">    
  40.                 Long Date :    
  41.             </td>    
  42.             <td style="width: 50%;">    
  43.                 @ViewData["longDt"]    
  44.             </td>    
  45.         </tr>    
  46.         <tr>    
  47.             <td style="width: 15%;">    
  48.                 Formatted Date:    
  49.             </td>    
  50.             <td style="width: 50%;">    
  51.                 @ViewData["formattedDt"]    
  52.             </td>    
  53.         </tr>    
  54.         <tr>    
  55.             <td style="width: 15%;">    
  56.                 Date :    
  57.             </td>    
  58.             <td style="width: 50%;">    
  59.                 @ViewData["fDt"]    
  60.             </td>    
  61.         </tr>    
  62.         <tr>    
  63.             <td style="width: 15%;">    
  64.                 Current Time Zone :    
  65.             </td>    
  66.             <td style="width: 50%;">    
  67.                 @ViewData["CurrentTimeZone"]    
  68.             </td>    
  69.         </tr>    
  70.         <tr>    
  71.             <td style="width: 15%;">    
  72.                 UTC Offset :    
  73.             </td>    
  74.             <td style="width: 50%;">    
  75.                 @ViewData["UTCOffset"]    
  76.             </td>    
  77.         </tr>    
  78.         <tr>    
  79.             <td style="width: 15%;">    
  80.                 Day Light Start Time :    
  81.             </td>    
  82.             <td style="width: 50%;">    
  83.                 @ViewData["dayLightStartTm"]    
  84.             </td>    
  85.         </tr>    
  86.         <tr>    
  87.             <td style="width: 15%;">    
  88.                 Converted Time :    
  89.             </td>    
  90.             <td style="width: 50%;">    
  91.                 @ViewData["ConvertedTZone"]    
  92.             </td>    
  93.         </tr>    
  94.     </table>    
  95. </div>   
Step 3: Now, run the Application and our result will be:


Similar Articles