Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am trying to move my C# code from .Net 3.5 to .Net 4.0. Facing a strange problem.
The following code worked well in .Net 3.5

DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")


But, for some reason, in the 4.0 run, it produces result like yyyy-MM-dd rather than yyyy/MM/dd . The '/' is replaced by '-' in the output.

I googled around for quite some time.. but no luck. Has someone seen this problem before?
Posted

Try the below code,

C#
DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo)


The problem is the convert function is using the separator from the Date/Time settings of your machine. The above code should work perfectly.


Mark it as answer if it is helpful
 
Share this answer
 
v2
Comments
Mehdi Gholam 11-Oct-11 0:07am    
my 5!
Venkatesh Mookkan 11-Oct-11 0:08am    
Thank you!
m@dhu 12-Oct-11 1:31am    
My 5!
Venkatesh Mookkan 12-Oct-11 1:32am    
Thank you!
[no name] 11-Oct-11 22:09pm    
Thanks Venkatesh! This worked!
Actually The Format Was Chaged In .Net Framework 4.0 Compare To .Net3.5 It Accepts Format as DateTime.Now.ToString("dd/MMM/yyyy") this format displays the date as
11/Oct/2011 and if changed the month to 2 MM i,e DateTime.Now.ToString("dd/MM/yyyy") and now the date will display in this format: 11/10/2011.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900