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

I have observed a scenario where DateTime.Now.ToString("MM/dd/yyyy") is working very fine if my System Date Format is MM/dd/yyyy(ControlPanel->Region and Language). But if I change the System Date Format to dd-MM-yyyy then DateTime.Now.ToString("MM/dd/yyyy") returning result in the form of dd-MM-yyyy.

Any reason of this different scenario and how can we solve this. I want that irrespect of any System Date Format my DateTime.Now.ToString("") should return the same result of what format I am trying to return.

See below example.
1)When System Date format is MM/dd/yyyy(ControlPanel->Regional and Language)
DateTime.Now.ToString("MM/dd/yyyy"); returning the date in proper format as expected.

1)When I change System Date format is dd-MM-yyyy(ControlPanel->Regional and Language)
DateTime.Now.ToString("MM/dd/yyyy"); returning the date in dd-MM-yyyy format, in spite of specifying the ToString("MM/dd/yyyy")
So my question is why this behavior change if I am changing the System Date format.


Please help
Thanks in Advance
Posted
Updated 10-Dec-14 2:21am
v4
Comments
PIEBALDconsult 10-Dec-14 8:11am    
That doesn't seem likely.
Got examples? Show some code.
Use Improve question to add detail.
Praveen Kumar Upadhyay 10-Dec-14 8:22am    
you want any more explanation??
PIEBALDconsult 10-Dec-14 8:32am    
Yes, please show the code and some examples.
Praveen Kumar Upadhyay 10-Dec-14 8:45am    
I have mentioned everything what I could help. This is a single line of code DateTime.Now.ToString("MM/dd/yyyy").
It is working fine if my system date format is MM/dd/yyyy but this only single line of code is not working properly if I am changing my system date format to dd-MM-yyyy
Tomas Takac 10-Dec-14 8:37am    
Actually yes, it's very hard to believe that DateTime.Now.ToString("MM/dd/yyyy") would return date in different format than specified. Are you sure you are not using DateTime.Now.ToString("d") in your code?

Your last comment about '/' being changed to '-' was correct! In date format slash (/) is just a placeholder for date separator[^]. Use backslash (\) to escape the slashes inyour format:
C#
DateTime.Now.ToString("MM\\/dd\\/yyyy");

Edit - specify culture
Alternativelly you can specify culture and use standard format:
C#
DateTime.Now.ToString("d", CultureInfo.InvariantCulture);
 
Share this answer
 
v2
Comments
Praveen Kumar Upadhyay 10-Dec-14 8:59am    
This is correct and it worked too but we usually don't do this. So can't we use a standard format which should work on all systems.
Tomas Takac 10-Dec-14 9:09am    
You can use standard format when you specify culture.
Praveen Kumar Upadhyay 10-Dec-14 9:13am    
Can you please explain the concept behind it. Why only '/' is getting converted to '-'. If I am using any other seperator then that is working fine.
Tomas Takac 10-Dec-14 9:18am    
Read the link! '/' is just a placeholder like 'dd' or 'MM' and gets replaced by the actual date separator for your current culture, which in your case is '-'.
Praveen Kumar Upadhyay 10-Dec-14 9:22am    
Thanks a lot for your help. Now I will have to change the separator everywhere in my code.
 
Share this answer
 

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