Home > front end >  DateTime.UtcNow.ToString("yyyy/MM/dd HH:mm:ss") gives different formats for different user
DateTime.UtcNow.ToString("yyyy/MM/dd HH:mm:ss") gives different formats for different user

Time:10-15

I assumed that ToString("yyyy/MM/dd HH:mm:ss") will force the string to be formatted with '/', but I can see that every device gets different formats. How can I force it to be saved with '/'?

Good example-

  • 2021/10/06 18:05:53

Strange examples I see in my DB from different users-

  • 2021-10-06 23:48:37

  • 2021.10.12 12:41:42

  • 2021. 10. 06 19:17:23 ('.' space after)

  • 2021.10.13 19.18.16

One solution is to replace every -, . and . to /, but this only solves the strange examples I found. What if there are others?

CodePudding user response:

/ in a format string means "the culture-specific date separator". If you want the literal forward-slash, quote it (and the colons, to avoid the use of a custom time separator):

ToString("yyyy'/'MM'/'dd HH':'mm':'ss")

Alternatively - and probably better - use the invariant culture. Not only will that use / as the date separator, but you won't need to worry about a culture having a different default calendar. (It'll always use the Gregorian calendar, which is presumably what you want.)

Even better, use an ISO-8601 format - you're already using a "slightly unusual for humans" format of year-first, so you might as well go the whole hog and go with the standard format for dates and times.

Sample code:

String text = dateTimeValue.ToString(
    "yyyy-MM-dd'T'HH:mm:ss",
    CultureInfo.InvariantCulture);

This is also the sortable standard date/time format so you can simplify the code significantly:

String text = dateTimeValue.ToString("s");

(That format always uses the invariant culture.)

That's if you really need to format the string at all, though. If you're saving it in a database, I'd advise you to:

  • Use an appropriate type in the database, e.g. DATETIME
  • Store it using a parameter (specifying the value just as a DateTime), not formatted text

If you do both of these, you'll avoid oddities like this.

CodePudding user response:

Another solution that I can think of is creating a new function that creates a date
DateTime date= DateTime.UtcNow; And extracting manually and splitting the date to a few strings (year,month,day,hour,month,seconds)

string year = date.Year.ToString(); string month = date.Month.ToString();... and building a string out of it in the right format, string newDate= year "/" month "/" day " " hour ":" minute ":" seconds; that way I can be sure it's always one format that I'll decide on

CodePudding user response:

How about storing the date as a number, eg unix time - DateTimeOffset.UtcNow.ToUnixTimeSeconds() or (DateTime.UtcNow - DateTime.UnixEpoch).TotalSeconds - it's a lot simpler and cheaper to store a number than a string

Also wanted to point out that the only date I've seen you store so far is UtcNow (as written in your answer) - fire base does appear to have a solution for that in that you can send ServerValue.TIMESTAMP and it will cause fb to store the unix time as the server sees it.

My take away from this (never used fb) is that that's how they store dates so perhaps it makes sense to follow :)

  • Related