Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using DataType Datetime how can i get the date format in dd/MM/yyyy while running on the local server getting in dd/MM/yyyy for but after publish on the server getting in MM/dd/yyyy format 


What I have tried:

Without using DateTime.Now.ToString(), instead any variable

1. Convert.ToDateTime(TodayDate_Greater, System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
2. DateTime.ParseExact(TodayDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Posted
Updated 5-Jun-18 19:13pm

1 solution

A DateTime doesn't "have" a format: it's stored internally as a number of ticks since a fixed point in time - and it only acquires a format when it is converted to a string for presentation.

The only way to specify how a DateTime value will be shown is to use ToString with a specific format specifier:
C#
string formatted = theDateAndTime.Tostring("dd/MM/yyyy");
Or to use a formatting string on a GridView by setting DefaultCellStyle.Format to "dd/MM/yyyy" (or similar for other controls).

Sorry - but that is how things work!
 
Share this answer
 
Comments
Khan Sameer 6-Jun-18 1:19am    
Actually the problem is while running on a local server i do the date calculation in dd/MM/yyyy format and after publishing on the server i have to convert in MM/dd/yyyy format as such.
V. 6-Jun-18 1:41am    
I think you misunderstood OriginalGriff's answer.
You do not do calculations in one format or the other.
You have a datetime. You can represent that datetime in any format you like. You can perform calculations on that datetime and NOT on it's representation. Then you can send that information (let's call it datetime 2) to another server and represent that datetime 2 any way you want. So datetime 1 and datetime 2 are both datetime types that can be represented in any format you like.
OriginalGriff 6-Jun-18 1:57am    
DateTime values do not have a format: I told you that.
When you store a Datetime any format it "had" prior to that point is discarded - which is a very good thing - and it only gets a "format" again when it is converted back into a string.

And that's a good thing: otherwise user input would be stored in user format and you would not be able to tell what data this represents: "11/12/13"
Is that 11th Dec 2013? 12th Nov 2013? 13th Dec 2011? Depends on who originally input it: a European, an American, or a Japanese user - and you don;t know that once the data is entered!
Khan Sameer 6-Jun-18 2:26am    
ok got the point. Thanks .....
OriginalGriff 6-Jun-18 2:32am    
You're welcome!

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