0

Needing expert advice for date format issue . I have a string value strDate= "03/04/2018" which is dd/mm/yyyy . I am trying to convert into format dd mmm yyyy . If I use (Convert.ToDateTime(strDate)).ToString("dd MMM yyyy") , this results in 4 Mar 2018 instead of 3 Apr 2018 .

Can anyone please suggest if this is dependent on the system and regional setting ? Is it possible to make it work independent of regional settings ?

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
Suzane
  • 193
  • 1
  • 3
  • 13
  • 3
    Call `DateTime.ParseExact`. – SLaks Mar 12 '18 at 22:43
  • 1
    Yes, I can confirm that it depends on your current culture. – itsme86 Mar 12 '18 at 22:43
  • 1
    You'll probably want to use [this](https://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.110).aspx) overload. – itsme86 Mar 12 '18 at 22:49
  • 2
    (Edited) `DateTime.ParseExact(strDate, "dd'/'MM'/'yyyy", null).ToString("dd MMM yyyy", CultureInfo.InvariantCulture)` for example. And if you want to be absolutely sure, use `DateTime.ParseExact(strDate, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("dd MMM yyyy", CultureInfo.InvariantCulture)`. – Jeppe Stig Nielsen Mar 12 '18 at 23:03
  • @itsme86 Yeah, I did not get it right at first. Edited the comment. – Jeppe Stig Nielsen Mar 12 '18 at 23:08

1 Answers1

0
var strDate = "03/04/2018";
var result = DateTime.ParseExact(strDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)
            .ToString("d MMM yyyy", CultureInfo.InvariantCulture);
Ryan Yuan
  • 2,396
  • 2
  • 13
  • 23