locked
convert a string to datetime 05.22.2018 10:31:14 RRS feed

  • Question

  • User632428103 posted

    Hello,

    i have this string : 05.22.2018 10:31:14

    => 05 is the month 

    => 22 it s the day

    how i can convert to a valid datetime

    i have try this but not working : 

    var x = DateTime.ParseExact("05.22.2018 10:08:18", "MM.dd.YYYY HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

    thanks for your help

    Tuesday, May 22, 2018 8:37 AM

Answers

  • User753101303 posted

    Hi,

    Depends a bit from where comes this string ? For now I would perhaps use :

    var x = DateTime.Parse("05.22.2018 10:08:18", System.Globalization.CultureInfo.InvariantCulture)

    Now it depends from where this string comes and what should happen if provided one day with some other format. Is this really supposed to be an InvariantCulture date ???

    Generally speaking my personal preference is to use the same culture used to create the string.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 22, 2018 11:49 AM
  • User-369506445 posted

    please try below code:

    string theDate = "05.22.2018 10:08:18";
                DateTime dt=   DateTime.ParseExact(theDate, "MM.dd.yyyy HH:mm:ss", null);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 22, 2018 11:54 AM
  • User753101303 posted

    A database stores a "native" date value using its own internal representation. A date have a format only when it is about to be shown to a user (and a db management tool could have its own format, you have tons of ways to show the SAME date value depending on which country convention is used).

    Usually you try either to work with a native date value all the way and let possibly a serialization layer to handle that for you or you use the invariant culture which is preferred for transporting date as text between programs (the invariant culture can't even be customized so you are sure it is the same on all computers).

    If the date is shown ina user ui using some culture, then you try to use the same culture when converting back the strng to a date value so that the user can use the convention for his country...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 22, 2018 1:20 PM

All replies

  • User-369506445 posted

    hi

    please try below code :

    DateTime dt = DateTime.Parse("05.22.2018 10:08:18");

    Tuesday, May 22, 2018 10:06 AM
  • User632428103 posted

    Hello vahid,

    i've try but not work ...

    i have do this but not sure if it's the best method :

    string theDate = "05.22.2018 10:08:18";
    DateTime dt = DateTime.ParseExact(theDate, "MM.dd.yyyy HH:mm:ss", CultureInfo.InvariantCulture);

    05 it's the month and 22 it's the day

    Tuesday, May 22, 2018 11:20 AM
  • User753101303 posted

    Hi,

    Depends a bit from where comes this string ? For now I would perhaps use :

    var x = DateTime.Parse("05.22.2018 10:08:18", System.Globalization.CultureInfo.InvariantCulture)

    Now it depends from where this string comes and what should happen if provided one day with some other format. Is this really supposed to be an InvariantCulture date ???

    Generally speaking my personal preference is to use the same culture used to create the string.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 22, 2018 11:49 AM
  • User-369506445 posted

    please try below code:

    string theDate = "05.22.2018 10:08:18";
                DateTime dt=   DateTime.ParseExact(theDate, "MM.dd.yyyy HH:mm:ss", null);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 22, 2018 11:54 AM
  • User632428103 posted

    Hello all,

    @patriceSec => this date is store on a device under android (sql lite) the format xx.xx.xxxx it's not the best i suppose but i need to keept it .

    your sample work

    @vahid  => ok thanks that's work

    thanks for your help

    Tuesday, May 22, 2018 12:47 PM
  • User753101303 posted

    A database stores a "native" date value using its own internal representation. A date have a format only when it is about to be shown to a user (and a db management tool could have its own format, you have tons of ways to show the SAME date value depending on which country convention is used).

    Usually you try either to work with a native date value all the way and let possibly a serialization layer to handle that for you or you use the invariant culture which is preferred for transporting date as text between programs (the invariant culture can't even be customized so you are sure it is the same on all computers).

    If the date is shown ina user ui using some culture, then you try to use the same culture when converting back the strng to a date value so that the user can use the convention for his country...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 22, 2018 1:20 PM
  • User632428103 posted

    Hello patriceSc, thanks for your explanation that's great to find any help 

    Wednesday, May 23, 2018 7:54 AM