Monday, April 11, 2011

.Net: DateTime String format

I'm trying to add a timestamp to a file. However the DateTime Format ToString() looks weird to me.

Here is my code:

Dim _timeStamp As String = Date.Now.ToString("dd_mm_yyyy")

but the value looks like this:

_timeStamp = "03_24_2009"

I checked my PC and the current date is correct. Shouldn't the value look like this: 03_03_2009?

From stackoverflow
  • For 2-digit month number, use MM not mm. Captial M's refer to month, lowercase m's refer to minutes.

    Also, John Sheehan has a nice cheat-sheet for such things.

    John Sheehan : Handy cheat sheet: http://john-sheehan.com/blog/net-cheat-sheets/
  • Try using MM for month. The .ToString is case-sensitive, and lowercase means minutes.

  • use MM, mm is for minutes

  • mm is minutes. You want MM for months.

  • As others have said, "mm" means minutes and "MM" means months.

    I find these MSDN pages useful:

    There are also various cheat sheets available on the web. I know I've seen a few of them around, but I only remember my own DZone refcard which includes it (also on Scribd).

    EDIT: As mentioned in the comments, John Sheehan has one too. Given that they're all free, why not download everything you can find and keep the one you find most useful? :)

    John Sheehan : Jon, I've put those pages into a cheat sheet: http://john-sheehan.com/blog/net-cheat-sheets/
    Jon Skeet : @John: So have I, effectively - see the edit I was making when you commented :) I'll add your link too.
  • I always get this right thanks to my mnemonic:

    Months are larger than minutes, "MM" is larger than "mm"

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.