0

I followed suggestion from vb.net convert string to date . But, it did not work.

the code is as follows:

    Dim Dt As DateTime
    If DateTime.TryParse("Thu, 09 Dec 2010 16:03:24 EST", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Dt) Then
        MessageBox.Show(Dt)
    End If

Can anyone solve this for me? I need to have date populated in the format of "yyyy-mm-dd hh24-mi-ss".

Community
  • 1
  • 1
user203687
  • 6,875
  • 12
  • 53
  • 85

4 Answers4

0
    TextBox1.Text = System.DateTime.Now.AddMinutes(698).ToString("dd/MM/yyyy");

    DateTime DOB;
    string[] formatsDOB = { "dd/MM/yyyy", "MM/dd/yyyy" };
    DateTime.TryParseExact(txtDateofBirth.Text, formatsDOB, CultureInfo.CurrentCulture, DateTimeStyles.None, out DOB);
  • Welcome to the site. You might want to consider adding some text to your answer describing what your code does as this will prove beneficial to those completely unfamiliar with the language. Code-only answers are generally not encouraged - a discussion of this can be found [here at meta-stackoverflow](http://meta.stackexchange.com/questions/148272/is-there-any-benefit-to-allowing-code-only-answers-while-blocking-code-only-ques). – Colin T Bowers Jan 10 '13 at 06:12
0

NOTE: this answer was written for a previous revision of the question which had the following code:

Dim Dt As DateTime
If DateTime.TryParseExact("Thu, 09 Dec 2010 16:03:24 EST", "dd.MM.yyyy", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Dt) Then
    MessageBox.Show(Dt)
End If

The format string that TryParseExact takes as it's second parameter specifies the format of the date in the string passed as the first.

In your case the format string is specifying that the date will be of the format "09.12.2010" for example - just the day, moth and year. However, as the string isn't in that format it won't parse. If you'd just used ParseExact it would have raised an exception.

The MSDN page for the variant of TryParseExact that takes an array of possible format strings has more examples, but non match your format exactly, but working with the format strings used to convert DateTime to string you probably want something like this:

"ddd, dd MMM yyyy HH:mm:ss ???"

but I can't find what you'd need instead of "???" to match the "time zone as string". You might have to do some string manipulation to remove this before calling TryParse or TryParseExact.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Sorry. My bad. I should have been clear. First of all, the code what I posted does not parse at all. – user203687 Dec 09 '10 at 23:56
  • @user203687 - it won't as the date time string doesn't match the format string. I obviously didn't make that clear. – ChrisF Dec 09 '10 at 23:58
  • oh. Ok. But Chris, I am unable to parse the following at least: Dim Dt As DateTime If DateTime.TryParse("Thu, 09 Dec 2010 16:03:24 EST", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Dt) Then MessageBox.Show(Dt) End If – user203687 Dec 10 '10 at 00:09
  • @user203687 - I think it's the time zone name that's causing the problem. I haven't found the format specifier for this yet. – ChrisF Dec 10 '10 at 00:11
  • @user203687 - see http://stackoverflow.com/questions/241789/parse-datetime-with-timezone-of-form-pst-cest-utc-etc – ChrisF Dec 10 '10 at 00:31
0

this should get you on the right path:

    Dim dt As DateTime
    dt = Now
    TextBox1.Text = Format(dt, "yyyy-MM-dd HH:mm:ss")
halfevil
  • 550
  • 2
  • 3
0

You will have to replace the timezone with the timezone offset. Same question as Parse DateTime with time zone of form PST/CEST/UTC/etc

Community
  • 1
  • 1
yorah
  • 2,653
  • 14
  • 24