Click here to Skip to main content
12,356,455 members (69,192 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

14.4K views
3 bookmarked
Posted

DateTime format in .NET 4.0

, 10 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
If you have different DateTime pattern (especially separator) in the locale
Lets assume I have the DateTime pattern "dd-MMM-yyyy" in my locale.

DateTime.Now.ToString("yyyy/MM/dd") working different in 3.5 & 4.0 framework.

In 3.5, it uses the pattern given in the parameter to convert the date to string. So, the output would be "2011/11/10" and it the expected result.

But in 4.0, this would return as "2011-11-10" which is not correct. 4.0 is using the current culture's ("-") separator instead of using the pattern's separator ("/") given in the parameter. To fix it, we need to pass IFormatProvider along with the ToString function.

Eg:
DateTime.Now.ToString("yyyy/MM/dd", DateTimeFormatInfo.InvariantInfo)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Venkatesh Mookkan
Team Leader
Australia Australia
He is from a small village in Tirunelveli, TamilNadu (India). He is fun loving guy. He like to travel a lot. Bikes/Cars/Mobiles are his fantasies.

He started his career as a BPO Data Entry Assistant and got chance to work as an ASP.NET Programmer in the same company after a year. And he move to the next company in 2006 as Software Engineer.

He worked as a Team Leader in a private software development company @Chennai, India for 8+years.

Now working as a Consultant @Sydney, Australia.

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 5 Something that is easy to miss and c... Pin
sp4mmed17-Oct-11 23:01
membersp4mmed17-Oct-11 23:01 
GeneralRe: Reason for my vote of 5Something that is easy to miss and c... Pin
Venkatesh Mookkan4-Sep-12 16:36
memberVenkatesh Mookkan4-Sep-12 16:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 10 Oct 2011
Article Copyright 2011 by Venkatesh Mookkan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid