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);
-
- var shortDT = defaultDate.ToString().Replace("12:00:00 AM", "");
-
- var userDt = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt");
-
- var nwDt = DateTime.Now.ToShortDateString();
-
- var nwTm = DateTime.Now.ToShortTimeString();
-
- DateTime dtByUser = DateTime.Parse(userDt).Date;
-
- var longDt = dtByUser.ToLongDateString();
-
- var shortTm = dtByUser.ToLongTimeString();
-
- var formattedDt = dtByUser.ToString("yyyy-MM-dd");
-
- var fDt = DateTime.Now.ToString("dddd, dd MMMM yyyy");
- 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);
- 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);
- 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: