int dy = DateTime.Now.Day; // integer between 1 and 31
// Optionally prepend a zero for dates smaller than 10
dy = DateTime.Now.Day.ToString("00"); // Screenshot ->
Note: "DateTime.Now.DayOfYear" returns the number of days elapsed
so far in the year (example: the first of February is the 32nd day).
Tip: for the name of the day (Monday, Tuesday...), use DateTime.Now.DayOfWeek. Returned data type is actually a DateTime object, not a string - and you'll get compile time error messages like "cannot convert from System.DayOfWeek to string" → get day names / abbreviations.
int mo = DateTime.Now.Month; // integer between 1 and 12
// ...And, to always show two-digit months:
mo = DateTime.Now.Month.ToString("00"); // 01 ... 12
Tip: you can also get the full month name (spelled out) in the user's or application's language.
<div id="footer"> © Copyright <%=DateTime.Now.Year%> Acme, Inc. <div>
... or get a two-digit year via the Substring() method (first convert to string: the year is an int).string twoDigitYear = DateTime.Now.Year.ToString().Substring(2); // 24
string curTime = DateTime.Now.Hour.ToString("00"); // Hours
curTime += ":" + DateTime.Now.Minute.ToString("00"); // Minutes
curTime += ":" + DateTime.Now.Second.ToString("00"); // Seconds
MessageBox.Show( "Current time is " +curTime ); // -> 05:12:07
DateTime nx = new DateTime(1970,1,1); // UNIX epoch date
TimeSpan ts = DateTime.UtcNow - nx; // UtcNow, because timestamp is in GMT
// Type-cast as integer to remove any decimals
MessageBox.Show( "UNIX timestamp = " + ( (int)ts.TotalSeconds ).ToString() );
Tip: you can check it against the PHP time() function, which currently returns "1734801127".