DateTime.ToLongDateString with different culture - c#

How can I convert a date(DateTime) to a string in a long format with the culture I need?
But the method(DateTime.ToLongDateString()) of converting date to long format string does not accept culture.
Long date string: "Wednesday, May 16, 2001"
In short, you can do this:
DateTime date = DateTime.Now;
date.ToString(CultureInfo.CreateSpecificCulture("en-US"));

ToLongDateString is using the format info's LongDatePattern, which is "dddd, MMMM d, yyyy".
So you can use:
date.ToString("dddd, MMMM d, yyyy", CultureInfo.CreateSpecificCulture("en-US"));
Or simpler, use the long date ("D") format specifier "D":
date.ToString("D", CultureInfo.CreateSpecificCulture("en-US"));
Result: Thursday, October 27, 2022

Related

What's the format date Node js (new date()) on C# [duplicate]

I have date object in JavaScript which give me: "Wed Oct 01 2014 00:00:00 GMT+0200";
I try to parse it but I get an exception:
string Date = "Wed Oct 01 2014 00:00:00 GMT+0200";
DateTiem d = DateTime.ParseExact(Date,
"ddd MM dd yyyy HH:mm:ss GMTzzzzz",
CultureInfo.InvariantCulture);
MM format specifier is 2 digit month number from 01 to 12.
You need to use MMM format specifier instead for abbreviated name of month.
And for your +0200 part, you need to use K format specifier which has time zone information instead of zzzzz.
And you need to use single quotes for your GMT part as 'GMT' to specify it as literal string delimiter.
string s = "Wed Oct 01 2014 00:00:00 GMT+0200";
DateTime dt;
if(DateTime.TryParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'K",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
Any z format specifier is not recommended with DateTime parsing. Because they represents signed offset of local time zone UTC value and this specifier doesn't effect DateTime.Kind property. And DateTime doesn't keep any offset value.
That's why this specifier fits with DateTimeOffset parsing instead.

Parse RSS pubdate to DateTime

I get this date string from an RSS:
Wed, 16 Dec 2015 17:57:15 +0100
I need to parse into a DateTime. Ive googled and searched stack overflow and gotten to the following answer (ive tried with only one, two and four 'z' instead of three)
string parseFormat = "ddd, dd MMM yyyy HH:mm:ss zzz";
DateTime date = DateTime.ParseExact(dateString, parseFormat,
DateTimeFormatInfo.CurrentInfo,
DateTimeStyles.None);
But I get this error:
System.FormatException: String was not recognized as a valid
DateTime.
Change your code to
string parseFormat = "ddd, dd MMM yyyy HH:mm:ss zzz";
DateTime date = DateTime.ParseExact(dateString, parseFormat,
CultureInfo.InvariantCulture);
Hope it helps!
As commented, your format and string matches unless if your CurrentCulture is english-based one. If it is not, it can't parse these Wed and Dec parts successfully.
On the other hand, zzz format specifier does not recommended for DateTime parsing.
From documentation;
With DateTime values, the "zzz" custom format specifier represents the
signed offset of the local operating system's time zone from UTC,
measured in hours and minutes. It does not reflect the value of an
instance's DateTime.Kind property. For this reason, the "zzz" format
specifier is not recommended for use with DateTime values.
However, I would parse it to DateTimeOffset instead of DateTime since you have an UTC Offset in your string like;
var dateString = "Wed, 16 Dec 2015 17:57:15 +0100";
string parseFormat = "ddd, dd MMM yyyy HH:mm:ss zzz";
DateTimeOffset dto = DateTimeOffset.ParseExact(dateString, parseFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None);
Now, you have a DateTimeOffset as {16.12.2015 17:57:15 +01:00} which as +01:00 Offset part.

DateTime.TryParse not working as expected

I am trying to get this date string 09 Apr 2015: 15:16:17 to display in this format 09/04/2015 15:16:17. This is what I have tried.
DateTime dtDateTime = new DateTime();
string dateString = "09 Apr 2015: 15:16:17";
DateTime dateValue;
DateTime.TryParse(dateString, out dateValue);
dtDateTime = dateValue;
This is the output 01/01/0001 00:00:00
I thought the TryParse would convert the dateString value to the required DateTime format. What am I doing wrong?
You should go with this:
DateTime dtDateTime = new DateTime();
string dateString = "09 Apr 2015: 15:16:17";
DateTime dateValue;
if (DateTime.TryParseExact(dateString, #"dd MMM yyyy':' HH':'mm':'ss",
new CultureInfo("en-us"), DateTimeStyles.None, out dateValue))
dtDateTime = dateValue;
Using TryParseExact you can provide a custom date format string to match your input date. In the example above I added that extra : after the year.
Also, you must use a CultureInfo which can understand your month name; here I assumed you got an english formatted date.
You need to specify the format since it's not a standard date format string:
DateTime.TryParseExact(
dateString,
"dd MMM yyyy: HH:mm:ss",
CultureInfo.CurrentCulture,
DateTimeStyles.None,
out dateValue);
Also, you should check the result of the call since TryParse and TryParseExact return true/false
You can use TryParseExact method:
DateTime.TryParseExact(dateString, "dd MMM yyyy: HH:mm:ss",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AllowWhiteSpaces,
out dtDateTime);
Tips:
If you use MMM, the month will be treated as if it is in 3 letter format (like Apr )
If you use HH rather than hh it means the hour part is in 24-hour format, the it will not fail on parsing 15 as hour

Parsing a complicated string as DateTime

Can someone tell me how should I approach converting the following format to a proper DateTime object?
11:50:46 AM on Wednesday, October 19, 2011
string s = "11:50:46 AM on Wednesday, October 19, 2011";
DateTime dateTime = DateTime.ParseExact(s,
"hh:mm:ss tt on dddd, MMMM dd, yyyy", CultureInfo.InvariantCulture);

C# How to convert String into Time and Date format?

I have a short program which converts a string to both date and time format from a simple string.
However it seems that the String is not recorgnizable for the system to be converted into date time format due to the sequence of the string. The String that should be converted is an example like : "Thu Dec 9 05:12:42 2010"
The method of Convert.ToDateTime have been used but does not work.
May someone please advise on the codes? Thanks!
String re = "Thu Dec 9 05:12:42 2010";
DateTime time = Convert.ToDateTime(re);
Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
Take a look at DateTime.TryParseExact
DateTime time;
if (DateTime.TryParseExact(re,
"ddd MMM d hh:mm:ss yyyy", CultureInfo.CurrentCulture,
DateTimeStyles.None, out time)) {
Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
} else {
Console.WriteLine("'{0}' is not in an acceptable format.", re);
}
It is often necessary to give it a hint about the specific pattern that you expect:
Edit: the double-space is a pain, as d doesn't handle that;
DateTime time = DateTime.ParseExact(re.Replace(" "," "),
"ddd MMM d hh:mm:ss yyyy", CultureInfo.CurrentCulture);
Take a look at DateTime.Parse
Try this
DateTime time = Convert.ToDateTime("2010, 9, 12, 05, 12, 42");
Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
Not sure if the string input should have the double space but you could pull that out and use geoff's answer.
re = Regex.Replace(re, #"\s+", " ");
Other option is to adjust his match string accordingly.
DateTime time = DateTime.ParseExact(re, "ddd MMM d HH:mm:ss yyyy", CultureInfo.CurrentCulture);

Categories