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();
}
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 );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With