Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date format different on server

When I execute the code below, I am getting my dates formatted as 04-07-2015 which is as expected what I want.

But when I execute the same code on another server, I am getting date as 7/4/2015. Why?

Here is my code on pageload:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        fillProject();
    }
    grdData.Visible = false;
    TxtIndate.Value = System.DateTime.Now.ToShortDateString();
    txtOutDate.Value = System.DateTime.Now.ToShortDateString();
}
like image 723
Nad Avatar asked Sep 14 '25 13:09

Nad


2 Answers

ToShortDateString method uses ShortDatePattern property of the CurrentCulture settings.

Probably your servers have different culture settings, that's why when you run this code, you get different string representations of your DateTime.

If you wanna get same representation in both server, set the same culture on both server in region and language settings or use custom date and time format specifiers like;

TxtIndate.Value = DateTime.Now.ToString("dd-MM-yyyy");

or as a better way as Matt mentioned, use InvariantCulture with string format like;

TxtIndate.Value = DateTime.Now.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture );
like image 153
Soner Gönül Avatar answered Sep 17 '25 03:09

Soner Gönül


From the c# code it seems impossible that the same code produces two different time formats. So as far as I can tell, we have to find it somewhere else.

Maybe the source of the confusion is in two different types of input controls in your HTML output. One type=text, one type=date that could explain the different formatting since the HTML 5 date control renders differently. So one date is the server formatted value, the other is the browser, and possibly client culture, formatted value.

like image 34
Patrick Hofman Avatar answered Sep 17 '25 03:09

Patrick Hofman