Skip to content

Instantly share code, notes, and snippets.

@ps-team
Created October 27, 2017 08:06
Show Gist options
  • Save ps-team/93a841a20eeb7f8f2639c037ca3fb580 to your computer and use it in GitHub Desktop.
Save ps-team/93a841a20eeb7f8f2639c037ca3fb580 to your computer and use it in GitHub Desktop.
DateTime formatting. For different formatting options please refer to the MDN docs: https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
@using Contensis.Framework.Web
@{
// Variables
var dateTime = Properties.time;
var day = dateTime.ToString("d ").Replace(" ", "");
var dateTimeOne = dateTime.ToString("yyyy-MM-ddTHH:mm");
var dateTimeTwo = dateTime.ToString("dd") + DateSuffix(day) + dateTime.ToString(" MMMM yyyy");
// Format one
<div>@dateTimeOne</div>
// Format two
<div>@dateTimeTwo</div>
}
@functions {
public string DateSuffix(string date) {
switch (date) {
case "1":
case "21":
case "31":
return "st";
case "2":
case "22":
return "nd";
case "3":
case "23":
return "rd";
default:
return "th";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment