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.            DateTime defaultDate = default(DateTime);                        // return 1/1/0001 12:00:00 AM  
  6.            var shortDT = defaultDate.ToString().Replace("12:00:00 AM"""); // return 08/05/2016 12:56 PM  
  7.            var userDt = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");       // return 08/05/2016 12:56 PM  
  8.            var nwDt = DateTime.Now.ToShortDateString();                     // return 08/05/2016 12:56 PM  
  9.            var nwTm = DateTime.Now.ToShortTimeString();                     // return 12:24 PM  
  10.            DateTime dtByUser = DateTime.Parse(userDt).Date;                // return 8/5/2016 12:00:00 AM  
  11.            var longDt = dtByUser.ToLongDateString();                       // return Friday, August 05, 2016  
  12.            var shortTm = dtByUser.ToLongTimeString();                      // return 12:00:00 AM  
  13.            var formattedDt = dtByUser.ToString("yyyy-MM-dd");              // return 2016-08-05  
  14.            var fDt = DateTime.Now.ToString("dddd, dd MMMM yyyy");          // return Friday, 05 August 2016  
  15.            ViewData["removeTm"] = shortDT;  
  16.            ViewData["nowDt"] = nwDt;  
  17.            ViewData["nowTm"] = nwTm;  
  18.            ViewData["userDt"] = dtByUser;  
  19.            ViewData["longDt"] = longDt;  
  20.            ViewData["shortTm"] = shortTm;  
  21.            ViewData["formattedDt"] = formattedDt;  
  22.            ViewData["fDt"] = fDt;  
  23.            TimeZone zone = TimeZone.CurrentTimeZone;  
  24.            ViewData["CurrentTimeZone"] = zone.StandardName;  
  25.            TimeSpan UTCOffset = zone.GetUtcOffset(DateTime.Now);                                   // return 05:30:00  
  26.            ViewData["UTCOffset"] = UTCOffset;  
  27.            System.Globalization.DaylightTime dayLightTm = zone.GetDaylightChanges(dtByUser.Year);  
  28.            ViewData["dayLightStartTm"] = dayLightTm.Start.ToString("hh:mm:ss tt");  
  29.            var s = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, zone.StandardName);     // return 8/5/2016 12:56:18 PM  
  30.            ViewData["ConvertedTZone"] = s;  
  31.            return View();  
  32.        }  
  33.    }  
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:

Up Next
    Ebook Download
    View all
    Learn
    View all