This question already has answers here:
How to convert Javascript datetime to C# datetime?
(17 answers)
Closed 6 years ago.
I would like to convert the following string to DateTime:
string start = "Wed Apr 27 2016 04:00:00 GMT+0300 (Jerusalem Daylight Time)";
Here you go:
string dateTimeString = "Wed Apr 27 2016 04:00:00 GMT+0300 (Jerusalem Daylight Time)";
string formatString = #"ddd MMM dd yyyy hh:mm:ss ""GMT""zzz ""(Jerusalem Daylight Time)""";
var parsedDateTime = DateTime.ParseExact(dateTimeString, formatString, System.Globalization.CultureInfo.InvariantCulture).ToLocalTime();
I stuck ToLocalTime() on the end so there's less confusion about what timezone it returns. Anything between double-quotes is a literal, everything else is a DateTime Format Specifier. Note that zzz is the UTC offset, not necessarily the GMT offset, but apparently GMT and UTC are effectively the same thing, so the code should be correct.
Related
This question already has answers here:
How to control appearance of ':' in time zone offset when parsing/formatting Datetime
(4 answers)
Closed 1 year ago.
I need to format current DateTime like this:
Mon, 18 Mar 2019 15:10:24 +0000
but i can't find the specific Format string in C#. The documentation required this string format:
EEE, dd MMM yyyy HH:mm:ss O
I try :
DateTime.Now.ToString("r") but it returns GMT instead of +0000 part.
DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zzz") but it returns +02:00 instead of +0200 part.
How can i format the date?
Try:
DateTime d = DateTime.Now;
d.ToString("ddd, dd MMM yyyy HH:mm:ss zz00")
This question already has answers here:
Parsing a Date with Month name to C# DateTime
(2 answers)
Closed 9 years ago.
How to parse following date-time string in c# DateTime object which is received from WebSphere in Linux environment.
string serverDate = "Sat Nov 03 13:03:13 GMT+05:30 2012"
Try this:
string serverDate = "Sat Nov 03 13:03:13 GMT+05:30 2012";
var date = DateTime.ParseExact(serverDate, #"ddd MMM dd HH:mm:ss \G\M\TK yyyy", CultureInfo.InvariantCulture);
Note how I had to escape each of the "GMT" characters separately.
How to format a JSON date obtained from twitter to a C# DateTime ?
Here is the format of the date I receive :
"Tue, 19 Feb 2013 13:06:17 +0000"
Can I do it with JSON.NET ?
Solved with use of DateTime.ParseExact
-> http://blog.kevinyu.org/2012/07/handling-json-in-net.html
Link Update: the linked blog post is offline. It cached copy can still be referenced via the Way Back Machine Internet Archive.
The common .NET code copied from the blog post is:
public const string Const_TwitterDateTemplate = "ddd MMM dd HH:mm:ss +ffff yyyy";
DateTime createdAt = DateTime.ParseExact((string)jo["created_at"],
Const_TwitterDateTemplate, new System.Globalization.CultureInfo("en-US"));
where
variable jo is a JSON object representing the created_at date property, but effectively the Twitter date string goes into this parameter
Part of code from flow's answer.
public const string Const_TwitterDateTemplate = "ddd MMM dd HH:mm:ss +ffff yyyy";
DateTime createdAt = DateTime.ParseExact((string)jo["created_at"], Const_TwitterDateTemplate, new System.Globalization.CultureInfo("en-US"));
The answers above that use the ffff format specifier seem to return the correct result, but technically this is wrong. ffff is the format specifier for ten thousandths of a second, and the +0000 in a Twitter date indicates the hours and minutes offset from UTC. See the format below:
string twitterTime = "Wed Feb 22 15:49:01 +0000 2017";
string twitterTimeformat = "ddd MMM dd HH:mm:ss zzz yyyy";
DateTime dateTime = DateTime.ParseExact(twitterTime, twitterTimeformat,
CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
Console.WriteLine(dateTime);
Result: 2/22/2017 3:49:01 PM
You can edit the DateTimeStyles enumeration to return the local time instead of UTC if desired.
Custom Date and Time Format Strings
DateTimeStyles Enumeration
It's DateTimeOffset not DateTime. Following should work.
DateTimeOffset parsed = DateTimeOffset.Parse("Tue, 19 Feb 2013 13:06:17 +0000");
I needed a PowerShell variant of these answers and the following worked for me.
PS> $Created_At = 'Fri Jun 05 18:15:48 +0000 2020'
PS> [datetime]::ParseExact($Created_At,'ddd MMM dd HH:mm:ss zzz yyyy',(Get-Culture))
Friday, June 5, 2020 1:15:48 PM
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Date time format from string?
Does anyone know how I could convert the following string to a DateTime value in C# ?
"Thu Nov 15 2012 00:00:00 GMT+0300 (E. Africa Standard Time)"
If you only have strings ending with "GMT+0300 (E. Africa Standard Time)", you can try:
string dateString = "Thu Nov 15 2012 00:00:00 GMT+0300 (E. Africa Standard Time)";
DateTime date = DateTime.ParseExact(dateString, "ddd MMM dd yyyy HH:mm:ss 'GMT+0300 (E. Africa Standard Time)'", System.Globalization.CultureInfo.InvariantCulture);
The meanings of the specifiers are as follows:
"ddd" The abbreviated name of the day of the week.
"MMM" The abbreviated name of the month.
"dd" The day of the month, from 01 through 31.
"yyyy" The year as a four-digit number.
"HH" The hour, using a 24-hour clock from 00 to 23.
"mm" The minute, from 00 through 59.
"ss" The second, from 00 through 59.
":" The time separator.
"string", 'string' Literal string delimiter.
You can find out more about different format specifiers in the MSDN article named Custom Date and Time Format Strings
Moreover, if you want to parse "GMT+0300 (E. Africa Standard Time)" part too, I think you should implement a way to parse them yourself. I don't think there's a specifier for that.
First of all, you should Africa Standart Time culture info use for yours';
CultureInfo( "af-ZA", false );
But your string is really complex for converting to DateTime. For me it looks imposible to convert to DateTime perfectly. But we can some rehabilitation in your string. For example, if your string was like this; "11/15/2012 00:00:00" you can convert it like this;
using System;
using System.Globalization;
namespace Programs
{
public class Program
{
public static void Main(string[] args)
{
string str = "11/15/2012 00:00:00";
DateTime dt = DateTime.ParseExact(str, "MM/dd/yyyy hh:mm:ss", new CultureInfo("af-ZA"));
Console.WriteLine(dt.ToString());
}
}
}
Custom Date and Time Format Strings
DateTime.ParseExact Method
Try this:
DateTime date = DateTime.Parse(yourDateTimeString);
There is no way to handle (E. Africa Standard Time).
Assuming that UTC=GMT you can also get the time zone part, just remove not important parts of your string
string t = Regex.Replace("Thu Nov 15 2012 00:00:00 GMT+0300 (E. Africa Standard Time)", "([(].+?[)])", "");
t= t.Replace("GMT", "").Trim();
DateTime a = DateTime.ParseExact(t, "ddd MMM dd yyyy HH:mm:ss zzzz", System.Globalization.CultureInfo.InvariantCulture);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse DateTime with timezone of form PST/CEST/UTC/etc
I have a datetime string = "10/09/2012 5:00 pm PST" How do I convert this into DateTime using DateTime.ParseExact(). I am looking for the literal that will match PST or EST.
Replace the PST with the UTC offset, I think it should work:
string value = "10/09/2012 5:00 pm PST";
value = value.Replace ("PST", "−8");
DateTime.ParseExact (value, "M/d/yyyy h:mm tt z", Culture....);