We are working on an application which will be available to customers in multiple regions including the UK and US. On a number of the view pages we display dates like this:
@Model.EventDate.ToString("dd/MM/yyyy HH:mm")
This has worked well in the UK however now that we are deploying a US version it should be something like (day/month switched around):
@Model.EventDate.ToString("MM/dd/yyyy HH:mm")
Obviously we don't want to have 2 codebases, 1 for the UK and 1 for the US.
Is there anyway we can render dates like this according to the region we are in?
My guess is that the first step would be rendering dates like this:
@Html.DisplayFor(n => Model.EventDate)
Then adding an attribute:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}" )]
Again the problems seems to be that the format is hardcoded in the attribute.
Any ideas?
DateTime.ToString method has a version with two arguments:
public string ToString(
string format,
IFormatProvider provider
)
plus, for the format argument supports formats that produce different results for different cultures - e.g. "d" value:
date.ToString("d", provider)
will produce 01.10.2008 for de-DE culture, but 10/1/2008 for en-US.
See Examples section of that MSDN article for more.
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