Hello, I am witting some software to interact with a mail order manager. Issue is they use a strange date/time format that they store as text in the DB, from what i guessed its YYYYMMDDHHMMSSNNN

right now im using this function to get it into C# as a standard date.

private static DateTime ProcessMwTime(string mwTime)
            //Mailware Date Format
            //YYYY/MM/DD HH:MM:SS.NNN
            //2009/09/16 11:41:35.766

            DateTime thisDate = new DateTime(int.Parse(mwTime.Substring(0, 4)),
                                             int.Parse(mwTime.Substring(4, 2)),
                                             int.Parse(mwTime.Substring(6, 2)),
                                             int.Parse(mwTime.Substring(8, 2)),
                                             int.Parse(mwTime.Substring(10, 2)),
                                             int.Parse(mwTime.Substring(12, 2))
            return thisDate;

Now the question, is there a better way to do this. to me it looks lame to have to read it like that.


Recommended Answers

All 6 Replies

Your way is fine but you could use TryParseExact :

private void button4_Click(object sender, EventArgs e)
     DateTime dt;
     if (DateTime.TryParseExact("20090916114135766", "yyyyMMddhhmmssfff", new System.Globalization.CultureInfo("en-US"), System.Globalization.DateTimeStyles.None, out dt))

Gah...beaten at the buzzer :p

I was about to post the same thing almost verbatim sknake.
Only difference was, i used

DateTime.ParseExact(date, "yyyyMMddHHmmssFFF", System.Globalization.CultureInfo.CurrentCulture);

note to self...must type quicker :)

commented: you got it ^5 +17

That happens to me too. Usually DdoubleD is the one who beats me :P

Great, thanks for the help, I tried using sknake first, but it was having issues parsing 4 of the dates (out of 15) but the format from Ryshad fixed that.

Thanks again guys


Please mark this thread as solved if you have found an answer to your question and good luck!

Just so you know for next time, the reason it failed on a couple was probably to do with the way the time was stored. sknakes format will work on times in 12-hour format (hh) mine will work with 24-hour format (HH).
"yyyyMMddhhmmssfff" "yyyyMMddHHmmssfff"

You can find a reference table showing the different components of the format string on msdn:

And like sknake said, turn out the light when your done (thread solved) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.