Local DateTime Format with suffix - c#

I'm currently trying to display a datetime in the format of the local culture of the user.
As an example, an user from the US will see a date as 20 June 2020 05.50 PM where as an user from germany will see the date as 20 Juni 2020, 17:50.
My issue is, that e.g for the german case, I'd like to have a Uhr added at the end, so it will be 20 Juni 2020, 17:50 Uhr as explained in this related question.
Is there any way to achieve that in .NET as well? In theory I could just keep checking the culture and add Uhr if it's a german one, but I assume that there are other cultures as well with a similar suffix.

Related

Adding months to a DateTime variable

I have a datetime variable with value 12-02-2019 (12th Feb 2019) - this is what i want. But in my code, it is in MM-dd-yyyy format. It saves to db in MM-dd-yyyy format (2nd Dec 2019). When i return it from Datebase, it will be like 02-12-2019 (2nd Dec 2019).
int salesid = (int)dr["SalesID"]; // dr is the datarow
DateTime salesdate = (DateTime)dr["SalesDate"]; // 02-12-2019 (2nd Dec)
And I want to add 4 months to 12-02-2019 (12th Feb). But the runtime adds 4 months to 02-12-2019 (2nd dec) and i am getting 02-04-2020 !!!
DateTime servicedate = salesdate.AddMonths(4); // 2020-12-02
This is wrong. I want to specify the salesdate as 12th Feb 2019 and ii should get 12th June 2019 after adding 4 months to the salesdate.
How this is possible in c# ?
When you save to the DB, make sure you are specify a more verbose format that the DB cannot confuse. For example, if you supply write to the database with myDateTime.Format("dd MMM yyyy"); then it will not confuse the months and days around.
This will make sure that the format in your code, and in you database, all stay aligned.
The problem is in your INSERT statement. While you should be passing the DateTime as-is to your database API, instead of using strings, if you want a simple fix, instead of calling ToString() on your CurrentSaleItem.SaleDate, use a different overload that lets you specify the culture and/or format explicitly, like ToString(string, IFormatProvider)

Re-format datetime to 7th November 2014

I have myself a DateTime variable of
7/11/2014
and I want to convert that date to display as
7th November 2014
What format do I use? I have tried ToLongDateString but it misses the suffix of the day date.
I don't believe there's any direct support for ordinals ("st", "nd", "th") within .NET. If you only need to support English, I suggest you hard code it yourself. For example:
string text = string.Format("{0}{1} {2} {3}", dt.Day, GetOrdinal(dt.Day),
dt.ToString("MMMM"), dt.Year);
(Where you'd write GetOrdinal yourself.) Note that this assumes you want exactly this format - different cultures (even within English) may prefer November 7th 2014 for example.
If you need to support all kinds of languages, it becomes very difficult - different languages have some very different approaches to ordinals.
Side-note: Even Noda Time doesn't handle this yet. I hope to eventually implement some CLDR support, which should in theory handle it for all locales. We'll see...

C# Parse string to date in the following format 19 March 2014 10:32:04 CEST [duplicate]

This question already has answers here:
Parse DateTime with time zone of form PST/CEST/UTC/etc
(6 answers)
Closed 8 years ago.
I am trying to convert strings to dates in the format below. I would like to keep the timezone format such that when i retrieve it from the DB it will be parsed to the correct time.
Another example of the string i receive is "19 March 2014 10:32:04 GMT";
`string date = "19 March 2014 10:32:04 CEST";
Console.WriteLine(date);
DateTimeOffset result = DateTime.ParseExact(date, "dd MMMM yyyy HH:mm:ss Z",
CultureInfo.InvariantCulture);' `
This is for a .net API
Thanks
Dan
The quick answer is, you can't do it.
Here is why,
There is a definitive database of world timezones, you can get it from the IANA here.
The problem is, the 3 or 4 letter abbreviations have a many-to-one association with the IANA timezones. For instance "AMT" means different things, depending on your culture or what part of the world you are in.
AMT "Armenia Time Asia" UTC + 4 hours
AMT "Amazon Time South America" UTC - 4 hours
If you really want to tackle this, I suggest using Noda Time to represent your Instances. You'll have to write some code to convert the abbreviations to a standard IANA timezone.
We can't do this for you, it depends on the context of your application.

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

Getting exception when using DateTime.Parse method

So, i have this string "Date: Mon Jan 03 2011 19:29:44 GMT+0200", and when i use DateTime.Parse(date).ToString(); i'm getting "String was not recognized as a valid DateTime."
If i remove the '+0200' part it works ok, but ofcourse it doesn't show the correct local time.
What's wrong with that?
From the documentation, it seems that DateTime.Parse() only understands:
The GMT designator, used alone, e.g. Mon, Jan 03 2011 17:29:44 GMT, or
A time zone offset specified without the GMT designator, e.g. Mon, Jan 03 2011 19:29:44+02:00.
You might want to convert your date string to the second form.
It just means that the time zone offset isn't an expected part of the default format strings.
If you know what format you're expecting, I suggest you call DateTime.ParseExact (or DateTime.TryParseExact), specifying the format(s) to try. Look at the documentation for custom date/time format strings for more details.
You have two mistakes.
First - don`t use Parse method. More correct is TryParse.
Second - you will have globalisation issues, when you use Parse or TryParse without arguments.
For example, see this code:
DateTime.Parse( "01.02.2011" ); In the USA it is 2nd of January. In the Germany it is 1st of February.
So, I recomment you to use formats from this article.

Categories

Resources