Date Time Formats Used in C# MVC

0
124

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:

public class DemoController : Controller  
   {  
       public ActionResult DtLocation()  
       {  
           DateTime defaultDate = default(DateTime);                        // return 1/1/0001 12:00:00 AM  
           var shortDT = defaultDate.ToString().Replace("12:00:00 AM", ""); // return 08/05/2016 12:56 PM  
           var userDt = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");       // return 08/05/2016 12:56 PM  
           var nwDt = DateTime.Now.ToShortDateString();                     // return 08/05/2016 12:56 PM  
           var nwTm = DateTime.Now.ToShortTimeString();                     // return 12:24 PM  
           DateTime dtByUser = DateTime.Parse(userDt).Date;                // return 8/5/2016 12:00:00 AM  
           var longDt = dtByUser.ToLongDateString();                       // return Friday, August 05, 2016  
           var shortTm = dtByUser.ToLongTimeString();                      // return 12:00:00 AM  
           var formattedDt = dtByUser.ToString("yyyy-MM-dd");              // return 2016-08-05  
           var fDt = DateTime.Now.ToString("dddd, dd MMMM yyyy");          // return Friday, 05 August 2016  
           ViewData["removeTm"] = shortDT;  
           ViewData["nowDt"] = nwDt;  
           ViewData["nowTm"] = nwTm;  
           ViewData["userDt"] = dtByUser;  
           ViewData["longDt"] = longDt;  
           ViewData["shortTm"] = shortTm;  
           ViewData["formattedDt"] = formattedDt;  
           ViewData["fDt"] = fDt;  
           TimeZone zone = TimeZone.CurrentTimeZone;  
           ViewData["CurrentTimeZone"] = zone.StandardName;  
           TimeSpan UTCOffset = zone.GetUtcOffset(DateTime.Now);                                   // return 05:30:00  
           ViewData["UTCOffset"] = UTCOffset;  
           System.Globalization.DaylightTime dayLightTm = zone.GetDaylightChanges(dtByUser.Year);  
           ViewData["dayLightStartTm"] = dayLightTm.Start.ToString("hh:mm:ss tt");  
           var s = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, zone.StandardName);     // return 8/5/2016 12:56:18 PM  
           ViewData["ConvertedTZone"] = s;  
           return View();  
       }  
   }

 

Step 2: Now, create one View for the above action and call all ViewData, which will show the date format results in UI.

@{  
    Layout = null;  
}  
<div style="width: 100%;">  
    <table style="width: 100%;">  
        <tr>  
            <td style="width: 15%;">  
                Remove Time From Default Date :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["removeTm"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                Todays Date :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["nowDt"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                Current Time :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["nowTm"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                User Date :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["userDt"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                Long Date :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["longDt"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                Formatted Date:  
            </td>  
            <td style="width: 50%;">  
                @ViewData["formattedDt"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                Date :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["fDt"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                Current Time Zone :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["CurrentTimeZone"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                UTC Offset :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["UTCOffset"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                Day Light Start Time :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["dayLightStartTm"]  
            </td>  
        </tr>  
        <tr>  
            <td style="width: 15%;">  
                Converted Time :  
            </td>  
            <td style="width: 50%;">  
                @ViewData["ConvertedTZone"]  
            </td>  
        </tr>  
    </table>  
</div>

 

Step 3: Now, run the Application and our result will be:

SHARE
Previous articleMaster Details View Using Telerik Grid For ASP.NET MVC
Next articleOLAP Features in SQL Server
Currently working as a Software Engineer in MNC company. Having working knowledge of technologies like ASP.NET, MVC 5.0, C#, Kendo UI, KnockOut.Js, JavaScript, SQL Server 2012, HTML5 etc. He had conducted more than 80 hands on Seminars, Workshops for College, Schools and Training Institutes. He blog at http://www.c-sharpcorner.com/members/rupesh-kahane2

LEAVE A REPLY

Please enter your comment!
Please enter your name here