Change Month name (Other language) in DateTime format dd/MMM/yyyy - c#

I have a dateTime field with the format dd/MMM/yyyy (05/NOV/2013). I want to change the "NOV" in some other language (e.g: Arabic). How can I convert it?
Edit: Sorry for not providing the code, here it is.
C# Code:
string tempGrgDt = Convert.ToString(DateTime.Now.ToString("dd/MMM/yyyy"));

You just need this
var date = DateTime.Now.ToString("dd/MMM/yyyy",new CultureInfo("ar"));
Prints
02/محرم/1435
You need to pass the CultureInfo while converting DateTime to String. You'll get localized string in the given culture.
Update:
If you have three letter month in english and you need to convert to arabic month you can do this
var dt = DateTime.ParseExact("NOV", "MMM", CultureInfo.InvariantCulture);
string arabicMonth = new CultureInfo("ar").DateTimeFormat.GetMonthName(dt.Month);

Related

Convert French Date to English Date C#

I have object that contains french date and i want to vert it to english date
Exp: from 21/01/1990 00:00:00 to 1990-01-21 00:00:00
This my code to convert the date but didn't work because the output is 21/01/1990 00:00:00, meaning it didn't convert the date.
if (obj.DATE_OPTIN.Equals("00/00/0000 00:00"))
obj.DATE_OPTIN = Convert.ToDateTime(obj.DATE_OPTIN_ORGANISATEUR).ToString("yyyy'-'MM'-'dd'T'HH':'mm'");
Where DATE_OPTIN is a string value.
You should convert it to a DateTime first specifying it's a french format and then display it as an english data by specifying the culture in the DateTime.ToString method.
You should have something like this:
using System.Globalization;
var frenchDateString = "21/01/1990 00:00:00";
Console.WriteLine($"French format: {frenchDateString}");
var dateTime = DateTime.Parse(frenchDateString, new CultureInfo("fr-FR"));
var englishDateString = dateTime.ToString(new CultureInfo("en-EN"));
Console.WriteLine($"English format: {englishDateString}");
To adapt your code to any culture, you can check this to find your culture code: https://learn.microsoft.com/en-us/bingmaps/rest-services/common-parameters-and-types/supported-culture-codes

How to convert date from one culture to another culture?

I have date with yy-MMM format in polish culture (pl-PL) and i want to convert that date in English (en-US)
Example
19-maj
Expected Output
19-may
Thanks in advance
If you have a string representing a datetime and you want to display the same information but in a different Culture then you could convert that string to a DateTime with the proper CultureInfo, then, once you get a datetime, you could output it in any format you like
string input = "19-maj";
CultureInfo ci = new CultureInfo("pl-PL");
DateTime dt = DateTime.ParseExact(input, "yy-MMM", ci);
string output = dt.ToString("yy-MMM", CultureInfo.InvariantCulture);
Console.WriteLine(output);

US vs Non US DateTime format

My program has to be able to compare not only us style vs us style format but also us style (mm/dd/yyyy) vs non us style (dd/mm/yyyy). How to do it? So far this is what I have and it only works to compare same style:
DateTime my_dt = new DateTime(); // this can be mm/dd or dd/mm
// depending on where it run
DateTime new_dt = Convert.ToDateTime(us_dt);
int compare = DateTime.Compare(new_dt, my_dt);
when my_dt is dd/mm, I got error :
System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at update.Program.Process(String ftp_path, String action)
Comparing the DateTime objects isn't the the real problem, it's the parsing. Given you have 2 strict formats here i.e. dd/mm/yyyy or mm/dd/yyyy the following should work
DateTime my_dt = null;
// parse in either US/Non-US format (culture-independant)
DateTime.ParseExact(someDateStr, new[] { "dd/MM/yyyy", "MM/dd/yyyy" }, CultureInfo.InvariantCulture, DateTimeStyles.None out my_dt);
// parse in US format (culture-dependant)
DateTime dt = DateTime.Parse(result3, new CultureInfo("en-US"));
// compare the results
int compare = DateTime.Compare(my_dt, result3);
Format is a property of datetime string representation, i.e. dt.ToString("mm/dd/yyyy").
System.DateTime is format agnostic, independent and unaware. So you can compare any two isntances of it.
Your question doesn't really illustrate what I think is your actual problem. I am guessing you have two date strings in different cultural formats and you want to compare them.
First of all, you need to know the culture or the format of the strings or else you could have unpredictable results.
Cultures can be identified by an LCID. You can find a list here.
So let's say you have a English (US) date string and a English (Canada) string, you could compare them like so:
string americanDateString = "12/31/2013";
string canadianDateString = "31/12/2013";
DateTime americanDate = DateTime.Parse(americanDateString, System.Globalization.CultureInfo.GetCultureInfo(1033); // 1033 = English - United States culture code
DateTime canadianDate = DateTime.Parse(canadianDateString, System.Globalization.CultureInfo.GetCultureInfo(4105); // 4105= English - Canada culture code
int compare = DateTime.Compare(americanDate, canadianDate);
EDIT: You can also use locale short strings (eg. "en-US" or "en-CA") to lookup the CultureInfo as per abatishchev's answer.

how can i assign only year say 2011 in a datetime variable in c#

I have a asp Text box as
where the user will fill only a year value, For this value I have Datetime type Property in c# application and Date type column in DB. So I want to convert that txtYear.Text to DateTime But it will only hold and/or show the year. Please help me in this situation.
A DateTime object will always hold a complete DateTime value, you can't use it to store a year only. (what use would that be anyway?) Besides, the datatype of a "year" is int, not DateTime.
So, I'd like to suggest changing your property to datatype int, both in your code and database.
To display just the year use the format "yyyy".
string s = "2011";
DateTime d = new DateTime(int.Parse(s), 1, 1);
Console.WriteLine(d.ToString("yyyy"));
Console.WriteLine(d);
You have to specify the format of the DateTime value you are manipulating:
String dateTimeFormat = "yyyy";
To show only a part of the DateTime value use the following:
dateTimeValue.ToString(dateTimeFormat);
To read a String value that represents a year into a DateTime use the following:
DateTime.ParseExact(stringValue, dateTimeFormat, CultureInfo.InvariantCulture);
DateTime.ParseExact Method (String, String, IFormatProvider) converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
DateTime.ToString Method converts the value of the current DateTime object to its equivalent string representation.
Rather than applying any string manipulating function make use of Year property. Check the documentation on the msdn by visiting below link.
DateTime.Year Property
A DateTime always has a full date component. When you create the DateTime instance, you'll need to assign a month and day, but you can ignore them in your usage.
DateTime d = new DateTime(int.Parse(txtYear.Text, 1, 1);
txtYear.Text = d.ToString("yyyy");
Even better would be not to use a DateTime but just use int. If you have only a year, you only need an int.
i assume the text box name is txYear
DateTime dt = new DateTime (Convert.ToInt32(txYear.text),1,1)
save this dt value in database
If you want only the year, why don't you make it of type smallint?
Anyway if you do really want to make it an year,
DateTime x = new DateTime(Convert.ToInt32(txtYear.text), 1, 1);
But make sure you validate that txtYear.text actually does have a valid year.
That is how I did and it worked.
string format = "yyyy";
var CurrentYear = DateTime.Now.ToString(format);

How to convert a date which is given in CCYYMMDD format to MM/DD/CCYY using C#?

Can anyone help me to convert a date which is given in CCYYMMDD format to MM/DD/CCYY format in C#.
Assuming by "CC" you mean century, you should just parse it and reformat it:
DateTime date = DateTime.ParseExact("yyyyMMdd", text,
CultureInfo.InvariantCulture);
string newText = date.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

Categories

Resources