Search notes:
System.DateTime (struct)
An instance of the
System.DateTime
represents
a point in time between 0001-01-01 and 9999-12-31
with an accuracy of 100 nanoseconds
without a reference to a time zone .
Each of these possible point in times is assigned a 64-bit number, called tick , which represents the number of 100-nonosecond intervals between Midnight January 1, 0001 and the point in time the instance of the struct represents.
String-formatting a DateTime
The ToString()
method takes a format string that allows to format the value of a DateTime
object.
The following
C# example shows a few format strings that are
culture-independent :
using System;
class C {
private static void printFormat(String format) {
Console.WriteLine("{0,-20}: {1}", format, DateTime.Now.ToString(format));
}
static void Main() {
printFormat("yyyy-MM-dd");
printFormat("HH:mm:ss" );
printFormat("r" ); // RFC 1123
printFormat("s" ); // sortable
printFormat("U" ); // universal
}
}
// possible Output:
//
// yyyy-MM-dd : 2019-11-09
// HH:mm:ss : 20:28:53
// r : Sat, 09 Nov 2019 20:28:53 GMT
// s : 2019-11-09T20:28:53
// u : 2019-11-09 20:28:53Z
$dt = new-object dateTime 2020,08,28 , 22,23,24
write-host "created date time refers to $('{0:yyyy-MM-dd HH:mm:ss}' -f $dt)"
Converting a UTC string to a local time
In
PowerShell , a string that describes a point in time in
UTC format can be converted to a local time like so:
$utc = '2021-10-25T13:23:08.0966497Z'
$local = ([dateTime] $utc).ToLocalTime()
write-host $local
Relation to file times
Ticks that are used in System.DateTime
are related to another Microsoft specific representation of point in times: file times .
A file time measures a point in time as the number of 100-nanosecond intervals («ticks») that have elapsed since midnight, January 1601,
UTC (as opposed to counting such intervals starting in the Year 1).
Removing time information from a DateTime object
With .NET 6, time related information (hours, minutes, seconds) can be «removed» from a DateTime
object:
var dt = DateOnly.FromDateTime(dt_hms);
Fields, properties and methods
Add()
AddDays()
AddHours()
AddMilliseconds()
AddMinutes()
AddMonths()
AddSeconds()
AddTicks()
AddYears()
Compare()
CompareTo()
Date
Day
DayOfWeek
DayOfYear
DaysInMonth()
Equals()
FromBinary()
FromFileTime()
FromFileTimeUtc()
FromOADate()
GetDateTimeFormats()
GetHashCode()
GetTypeCode()
Hour
IsDaylightSavingTime()
IsLeapYear()
Kind
indicates whether the time represented by this instance is based on local time, UTC or neither.
MaxValue
one of the three fields
Millisecond
Minute
MinValue
one of the three fields
Month
Now
Parse()
ParseExact()
Second
SpecifyKind()
Subtract()
Ticks
TimeOfDay
ToBinary()
Today
A static property that evaluates to a DateTime
that represents today .
ToFileTime()
ToFileTimeUtc()
ToLocalTime()
ToLongDateString()
ToLongTimeString()
ToOADate()
ToShortDateString()
ToShortTimeString()
ToString()
ToUniversalTime()
TryFormat()
TryParse()
TryParseExact()
UnixEpoch
one of the three fields
UtcNow
Year