According to Custom date and time format strings, you should use something like that
string DateAndTime = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss.sssz");
It will give you the following string 2020-04-04T11:19:06.06+3
yyyy
means four-digit year (YYYY
format doesn't exist), dd
represents the day of the month (from 01 to 31). z
is used for offset between your local time zone and UTC
The capital Z
literal in string indicates that datetime is in UTC format (according to ISO 8601 standard). To get it literally in a result string you may use DateTime.UtcNow
instead of DateTime.Now
with Z
literal or K
specifier at the end
string DateAndTime = DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss.sssK");
It gives you 2020-04-04T08:49:22.22Z
(current datetime in UTC format). K
specifier will add Z
literal for UTC dates and offset (in zzz
format) for local dates