format datetime based on users culture - c#

How can I format the date and time depending on the users region settings in an ASP.NET-MVC application without worrying about the order of the date?
For example, I want to have:
the day with a leading zero (dd);
month abbreviated three-letter form (MMM);
full year (yyyy);
the time just the hours and minutes both with leading zeros (HH:mm);
depending on if the user is from USA show AM/PM after the time;
Every country displays the date in a different order. USA displays first the month than days than years (MMM/dd/yyyy). In China first the year than month than day (yyyy-MMM-dd) (IIRC). And in Europe most countries display the date in this format: dd-MMM-yyyy.
And then not to mention the slashes/dashes used to separate the month, days and years from each other in every country.
This of course can be done with an endless if/else or switch statement, but isn't there a more elegant way to do this?
EDIT this is the best I came up with:
var cltr = System.Threading.Thread.CurrentThread.CurrentCulture;
DateTime.Now.ToString(cltr.DateTimeFormat.ShortDatePattern + " " + cltr.DateTimeFormat.ShortTimePattern)
It displays the date in numbers only. How would I change that in short month notation but not changing the order and the separators etc?

You can get the culture currently used by the user with CurrentCulture and CurrentCultureInfo.
For more details, see: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

I think you are talking about formatting issue. Once you have your DateTime object, then you can display it as you want. You have to specify new CultureInfo("en-US") as IFormatProvider in the DateTime.ToString() method if you want to show the datetime as US format.

Related

Angular UTC Date is showing one day after

I am storing date and time separately from Angular to C#. While storing in database, I combine start date and start time field in C# and store it in dAtabase UTC like this: "2020-02-28T22:30:30Z".
While returning from c#, i create a new DateTime with start date and start time and return as one variable. However, if the date is 28/02/2020 and time is 4.00 am, with timezoneoffset of 5.30 India, the date gets rendered to 29/02/2020 4.00 am.
Is it possible to get date and time in Angular and render it separately as string etc.
Thanks
In JavaScript Date is a timestamp, counts the number of miliseconds since January 1, 1970 00:00 UTC. So you might be having a problem with timezones. Check this answer I gave a couple of weeks ago, it may guide you. I also add a function to solve it and some references there, Subtract day in conversion between Moment and LocalDate.
angular set timezone based on user browser timezone so you can change datetimes to another timezone. please read it How to convert Date in angular to another time zone

DateTime.Now.ToString("MM/DD/YYYY") contains incorrect month

So I have been adding a DateTime.Now.ToString("MM/DD/YYYY") to a List (along with a bunch of other data) and the later writing these lists to individual rows in an Excel workbook.
This all works great (it's something I do regularly), except, the month. I have tried exporting it three times and each time I get a different month;
the first time 56/26-17,
the second 2/26/2017,
and the third 14/26/2017....
Use this instead:
DateTime.Now.ToString("MM/dd/yyyy")
"MM" for month. "dd" for days. "yyyy" for year.
"MM/DD/YYYY" is wrong format:
Console.WriteLine(DateTime.Now.ToString("MM/DD/YYYY")) // prints "06/DD/YYYY"
DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
As already answered you need to have format specifier for day and year in small letters but as an add on you should also specify the culture variable to make sure that final output contains "/" between date parts. Without this culture parameter "/" could be replace by the date separator of the culture of system where code is running like "-".

DateTime.Now.ToUniversalTime() Has the Wrong Year

And Idea Why the Year appears as 2555?
The culture of the site is Thai
Yes - any time DateTime is converted to a string with no explicit culture specified, it will use the current culture's calendar system. However, the DateTime components themselves still reflect the Gregorian calendar.
You'll see the 2555 if you use:
int thaiYear = new ThaiBuddhistCalendar().GetYear(DateTime.Now);
Basically, if you want to get culture-specific date information programmatically, you need to use a System.Globalization.Calendar. When formatting a date, make sure you specify the right culture for the calendar you want to use.
The Thai have a different calendar.
Let me quote:
There is a 543 years difference between the Buddhist calendar and the
Gregorian calendar. Year 2012 in Europe is year 2555 in Thailand.
From http://www.thaiworldview.com/feast/feast.htm

Set specific date format in c# for windows phone

Relatively new Windows Phone developer here, looking for some help. Basically I'm messing around with an app that I'm looking to eventually put on the marketplace.
Basically the app is counting down to a specific date, I've got the countdown working with no problems, however i do have a problem with the date format as I'm in the UK and the date format is dd/MM/yyyy whereas the states is MM/dd/yyyy. So the app goes into negative figures for anyone in the US. Basically i need help with some sort of workaround, whether it's setting a universal date format for my app or something like that. Here is the code for the countdown:
DateTime startDate = DateTime.Now;
var launch = DateTime.Parse("01/08/2012 00:00:00 AM");
TimeSpan t = launch - startDate;
Countdown.Text = string.Format("\r {0}\r Days \r {1}\r Hours \r {2}\r Minutes \r {3}\r Seconds", t.Days, t.Hours, t.Minutes, t.Seconds);
If you’re hard-coding the date, then you should use the DateTime(int,int,int) constructor, rather than parsing it from a string. The three parameters would always be interpreted as year, month, day (in that order).
var launch = new DateTime(2012, 08, 11);
You can also parse and format dates for other cultures by setting the current culture or Using a CultureInfo to format the string.
See http://msdn.microsoft.com/en-us/library/5hh873ya.aspx for more info.
The ToString() method for DateTime has an overload that takes a custom format string.
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
It would be fairly simple to ask for, or establish the users region, and send dates to him in the correct format.
Beyond that, there is an overload of ToString that takes a System.Globalization.CultureInfo object so you can do something like.
myDateTime.ToString(new System.Globalization.CultureInfo(Thread.CurrentThread.CurrentCulture.Name));
This should give you the datetime formatted correctly for the users region based on OS settings.

Format C# DateTime

I have a bit of code that display the date within a text field as shown below
textField.Text = DateTime.Now.ToShortDateString();
It shows as
11/03/2011
anyone know how I could format here to show it as
11/03/11
Thanks in advance
Yes. Take a look at this Date and Time formatting page.
Or: theDate.ToString("dd/MM/yy")
Very simple:
string strFormat = "dd/MM/yy";
textField.Text = DateTime.Now.ToString(strFormat);
note that the format string is case-sensitive, make sure you use capital 'M's for month, otherwise it will consider 'minutes' for 'm'.
More general help about datetime formatting:
MMM: display three-letter month
MM: display two-digit month
ddd: display three-letter day of the WEEK
d: display day of the MONTH
HH: display two-digit hours on 24-hour scale
mm: display two-digit minutes
yyyy: display four-digit year
DateTime.Now.ToString("dd/MM/yy")
DateTime.Now.ToString("dd/MM/yy")
But you need to remember that ToShortDateString() is culture sensitive, returning different strings depending on the regional settings of the computer - the above is not.
You could change the settings on your computer, in Windows 7, you will find the Short Date format under Region and Language in the control panel.
Here is an alternative if you don't like format strings.
var fp = new System.Globalization.CultureInfo("en-GB");
textField.Text = DateTime.Now.ToString(fp.DateTimeFormat.ShortDatePattern);

Categories

Resources