This question already has answers here:
Parse DateTime with time zone of form PST/CEST/UTC/etc
(6 answers)
Closed 9 years ago.
When I tried to convert PDT time to en-US culture it is not working.
DateTime.Parse("Wed, 08 Jun 2012 12:14:14 PDT",new CultureInfo("en-US"));
But this is working
DateTime.Parse("Wed, 08 Jun 2012 12:14:14",new CultureInfo("en-US"));
You can't use time-zones in DateTime.Parse(), but you can handle them separately.
Try this:
var time = DateTime.Parse("Fri, 08 Jun 2012 12:14:14",new CultureInfo("en-US"));
var zone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var pdtTime = TimeZoneInfo.ConvertTimeFromUtc(time, zone);
Update 1: Oops, got this the wrong way around. Here is the (correct) counterpart:
var enUsTime = TimeZoneInfo.ConvertTimeToUtc(pdtTime, zone);
Oh, and by the way - your original example will not work because 08 Jun 2012 is not a Wed, but rather a Fri... ;)
Update 2:
If all you need is a TimeDate, then you can parse is as follows (lin
using System.Linq; // Include a ref to LINQ
(...)
var originalDate = "Fri, 08 Jun 2012 12:14:14 PDT";
// Use LINQ to fetch the first 25 characters (ignoring "PDT"):
var dateToParse = new string(originalDate.Take(25).ToArray());
var result = DateTime.Parse(dateToParse, new CultureInfo("en-US"));
Related
I have string in "Mon, 20 Mar 2021 14:04:48 +0000"
and I want to convert it as "20 Mar 2021 | 14:04 PM"
I want to convert the string as it is but it was appearing differently in my local and server.
First of all 20 Mar 2021 is Saturday, not Monday, let's correct it. Then you can ParseExact to get DateTime and finally represent it in the required format with a help of ToString():
string source = "Sat, 20 Mar 2021 14:04:48 +0000";
string result = DateTime
.ParseExact(source, "ddd, dd MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture)
.ToUniversalTime()
.ToString("dd MMM yyyy' | 'HH:mm tt", CultureInfo.InvariantCulture);
Notes:
It seems that you want to obtain Universal (not Local) time, that's why I've added ToUniversalTime()
14:04 PM looks strange for me (14:04 and 02:04 PM are much more frequent formats); put hh instead of HH to have 02:04 PM
If you actually want to be manipulating the timezone information, then use Noda Time.
If that's your exact text format, and the dates and times are what you want, you can convert it manually:
var input = "Mon, 20 Mar 2021 14:04:48 +0000";
var dateParts = input.Split(' ');
var timeParts = dateParts[4].Split(':');
var amPm = int.Parse(timeParts[0]) < 12 ? "AM" : "PM";
var output = $"{dateParts[1]} {dateParts[2]} {dateParts[3]} | {timeParts[0]}:{timeParts[1]} {amPm}";
System.Console.WriteLine(output); // "20 Mar 2021 | 14:04 PM"
Or, if you are feel adventurous, use a regular expression.
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.
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.
I have the data in string format
"22:48:32 Jul 29, 2011 PDT"
Now I want to convert it into datetime.
How can I do it.
You are probably after this method:
DateTime.Parse(string)
PDT is -7 from GMT, so you can refer to the GMT and subtract 7 hours.
DateTime datetime = DateTime.Parse("Jul 29 2011 22:48:32 GMT"); // notice GMT
datetime = datetime.AddHours(-7); // minus 7 hours
I have a string in format
Jul 13 2011 1:07PM
I want to cast it as
dd/MM/yyyy HH:mm tt
e.g: 13/7/2011 11:49:00 AM //string=Jul 13 2011 1:07PM
I am using following code to cast it to date.
DateTime date = Convert.ToDateTime(Convert.ToDateTime(myDateString).ToString("dd/MM/yyyy HH:mm:ss"));
This works fine if my day in my string is less than 13
Jul 12 2011 1:07PM //this will cast to desire format fine!
Jul 13 2011 1:07PM //gives error String was not recognized as a valid DateTime.
I understand that it is taking day as month but I can not found a way to cast it to desire format.
See DateTime.ParseExact :
DateTime date = DateTime.ParseExact(myDateString, "MMM dd YYYY H:mmtt", CultureInfo.InvariantCulture);
See also Time Format Strings
You should use DateTime.TryParse
DateTime dt ;
if (DateTime.TryParse("Jul 13 2011 1:07PM",out dt))
MessageBox.Show("Converted to Date object");
Post that you use the ToString() method to get the desired output
dt.ToString("dd/MM/yyyy HH:mm")
First, convert the string Jul 13 2011 1:07PM to a date:
var date = Convert.ToDateTime("Jul 13 2011 1:07PM");
Then, convert it to a string in the format you like:
var dateText = date.ToString("dd/MM/yyyy HH:mm:ss");
I believe you're searching for this:
Date.ParseExact("Jul 13 2011 1:07PM", "MMM d yyyy h:mmtt", Globalization.CultureInfo.InvariantCulture)