Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Date Time Formats Used in C# MVC
WhatsApp
Rupesh Kahane
4y
244.7k
0
4
100
Article
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()
{
// return 1/1/0001 12:00:00 AM
DateTime defaultDate =
default
(DateTime);
// return 08/05/2016 12:56 PM
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 12:24 PM
var
nwTm = DateTime.Now.ToShortTimeString();
// return 8/5/2016 12:00:00 AM
DateTime dtByUser = DateTime.Parse(userDt).Date;
// return Friday, August 05, 2016
var
longDt = dtByUser.ToLongDateString();
// return 12:00:00 AM
var
shortTm = dtByUser.ToLongTimeString();
// return 2016-08-05
var
formattedDt = dtByUser.ToString(
"yyyy-MM-dd"
);
// return Friday, 05 August 2016
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;
// return 05:30:00
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"
);
// return 8/5/2016 12:56:18 PM
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:
C#
Date Time Formats
MVC
Up Next
Ebook Download
View all
ASP.NET GridView Control Pocket Guide
Read by 10.7k people
Download Now!
Learn
View all
Membership not found