2

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456

5 Answers5

3

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");
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
2

EDIT:

 TimeStamp.Text =  BlogComment.Date.ToUniversalTime().ToString(@"yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
Bala R
  • 107,317
  • 23
  • 199
  • 210
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fun Mun Pieng
  • 6,751
  • 3
  • 28
  • 30
  • 1
    There is one important reason. The `:` is understood to be the (invariant) time separator in a DateTime format string. When not escaped, the `:` may be transformed to the target cultures `DateTimeFormat.TimeSeparator`. Try your output by specifying the culture as Italian (`it-IT`) and you will see that that time is `2011-03-07T02.03.58.2693372+13:00` – Robert Paulson Mar 07 '11 at 02:07
  • Instead of escaping, it would be better to use the invariant format provider `DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz", DateTimeFormatInfo.InvariantInfo)` – Robert Paulson Mar 07 '11 at 02:57
  • Sorry I need to correct myself. For .net DateTime formatting, `/` is the invariant date separator, and `:` is the invariant time separator. – Robert Paulson Mar 07 '11 at 03:19
0

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

pasx
  • 2,718
  • 1
  • 34
  • 26
0

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'

Community
  • 1
  • 1
Robert Paulson
  • 17,603
  • 5
  • 34
  • 53