3

I'm trying to convert datetime format to MM/dd/yyyy but I get result as "05/14/14" which I'm expecting to be "05/14/2014".

What's wrong in this code?

string input = Datetime.Now.ToString("MM/dd/yyyy");     
DateTime d;

if (DateTime.TryParseExact(input, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d))
{
  // use d
}

Update I had short date setting in my pc to "MM/dd/yy" when I changed to "MM/dd/yyyy", it worked. What could be the solution so that datetime should show regardless of pc setting.

enter image description here

leppie
  • 115,091
  • 17
  • 196
  • 297
user8103
  • 309
  • 2
  • 3
  • 7
  • 7
    There is no problem with this code. I run it and string input == '14/05/2014' – Menelaos Vergis May 14 '14 at 10:08
  • When you parse a `string` to a `DateTime`, you are left with a pure `DateTime` object with no concept of format - in your case, exactly the same as what `DateTime.Now` returned. You need to show us how you are *converting `d` to a `string`* in order for us to help you. – Rawling May 14 '14 at 10:08
  • 2
    TryParseExact its to parse a string with the specific format to Datetime, if you want to return as MM/dd/yyyy, just do d.ToString("MM/dd/yyyy"). – Sílvio N. May 14 '14 at 10:09
  • @SílvioN.: but I want it to be converted in datetime not as string. – user8103 May 14 '14 at 10:12
  • 2
    No, you're not getting "05/14/14". You're getting a `DateTime` object. Don't confuse that with its representation, which can have any format. Also: `I want to be converted` is kinda creepy :) – Tarec May 14 '14 at 10:12
  • @MenelaosVergis: any idea why it's different format for me? Is that because I have a custom date setting for my pc which is "MM/dd/yy"? How I can avoid this so that format will be based on only code format which I write? – user8103 May 14 '14 at 10:20
  • 1
    the default presentation (when you console.WriteLine it) of DateTime object depends on your CultureInfo, since InvariantCulture is by default en-US, try to set Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); before everything. – Sílvio N. May 14 '14 at 10:21
  • @SílvioN. [`InvariantCulture` isn't en-US](http://stackoverflow.com/questions/2329297/net-are-there-any-differences-between-invariantculture-and-en-us), even for dates. – Rawling May 14 '14 at 10:23
  • Where are you looking such that you see `"05/14/14"`? – ClickRick May 14 '14 at 10:23
  • @Rawling You'r right, my mistake. – Sílvio N. May 14 '14 at 10:24
  • 1
    @ClickRick: right after convertion "DateTime d" using debug statement. Also, please check my update. – user8103 May 14 '14 at 10:32
  • @user8103 thank you for giving this update, I would never imagine that DateTime.ToString('..') depends to local settings. I wander if this happens only for 'MM/dd/yyyy' or it may happen to another format such as 'MM.dd.yyyy' – Menelaos Vergis May 14 '14 at 10:55
  • @MenelaosVergis ToString is not local dependent. The debug output is! – cubuspl42 May 14 '14 at 14:17
  • @cubuspl42 So he was referring to debug output and not to the string input! Thanks for pointing this out to me – Menelaos Vergis May 15 '14 at 07:25

1 Answers1

3
string input = Datetime.Now.ToString("MM/dd/yyyy");

should be DateTime.

With this change the code runs fine and gives desired output.No need to change the date format in PC. Also why to do all these stuff while this can be achieved with a single line of code:

var shortDate = DateTime.Now.ToString("d");

This will give the following output as "MM/dd/yyyy" irrespective of PC date format that you have mentioned.

Bas
  • 4,423
  • 8
  • 36
  • 53
Satya Ranjan Sahoo
  • 1,086
  • 7
  • 24