Reputation: 61773
Consider this code:
TimeStamp.Text = BlogComment.Date.UtcNow.ToString("yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
BlogComment.Date
is a DateTime
object with its date set. TimeStamp
is just a literal.
I keep getting unrecognised escape sequence. How can I fix this problem?
Upvotes: 2
Views: 3820
Reputation: 6911
Try this:
Stamp.Text = BlogComment.Date.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz");
Or
Stamp.Text = BlogComment.Date.ToString(@"yyyy-MM-ddTHH:mm:ss.fffffffzzz");
My mistake: The \
is indeed required, because it might be a custom format specifier.
And if you just want the current time, use
Stamp.Text = DateTime.UtcNow.ToString(@"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
UtcNow is static. It should not be accessed from an instance. It should be accessed from the class itself.
Alternatively, you might want:
Stamp.Text = BlogComment.Date.ToUniversalTime().ToString(@"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
This would get you the universal time of the Date in BlogComment.
Upvotes: 1
Reputation: 2985
DateTime.UtcNow..ToString("o");
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
http://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.85).aspx
Upvotes: 0
Reputation: 18081
When dealing with escaping characters in a DateTime format string, there's a similar question where @Oppositional's writes
When using custom format strings with a DateTime, it is important to remeber that you need to escape your seperators using single quotes.
string time = DateTime.UtcNow.ToString(
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz",
DateTimeFormatInfo.InvariantInfo);
The characters surrounded by the single quotes '
are literal strings inside a DateTime format string.
However, escaping the -
and :
is redundant if you are also specifying the Invariant culture. You should always specify the culture when formatting dates and times.
It's also important to note that /
and :
are the (invariant) date and time separators in a DateTime format string. DateTime.ToString will use the current cultures to transform them as necessary. For example, Italian (it-IT
) has the .
as its time separator. Similarly fr-CH
has a date separator of .
You can see this escaping in action if you take a look at System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.UniversalSortableDateTimePattern
which is yyyy'-'MM'-'dd HH':'mm':'ss'Z'
Upvotes: 0
Reputation: 161012
You want a string literal - prefixing a string with @ will not parse the string for escape sequences like you have in your string but take it in "literal" form.
@"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz"
Edit:
Also there is no UtNow
property on DateTime - this is a static property only available on the DateTime class. You can just write:
TimeStamp.Text = BlogComment.Date.ToString(@"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
or if your intention was to convert the time to UTC:
TimeStamp.Text = BlogComment.Date
.ToUniversalTime()
.ToString(@"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
Upvotes: 2
Reputation: 109027
EDIT:
TimeStamp.Text = BlogComment.Date.ToUniversalTime().ToString(@"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
Upvotes: 1