Parse DateTime issue in C# [duplicate] - c#

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.

Related

C# Format specific Datetime to string [duplicate]

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")

Get PM or AM from string date C# [duplicate]

This question already has answers here:
How do I format a DateTime in a different format?
(2 answers)
Closed 4 years ago.
I have this string as a date and I want to add the AM/PM to the end.
From
10-17-2018 00:00:00
To
10-17-2018 00:00:00 AM
How is possible?
Thanks a lot!
So convert the string date into a real DateTime, then ToString() it back out in the format you like:
var dateStr = "10-17-2018 00:00:00";
var date = DateTime.Parse(dateStr);
Console.WriteLine(date.ToString("MM-dd-yyyy HH:mm:ss tt"));
Output: 10-17-2018 00:00:00 AM
DateTime formats: http://www.csharp-examples.net/string-format-datetime/

C# convert complicated string to DateTime [duplicate]

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.

DateTime Conversion Error when I tried to convert PDT time [duplicate]

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"));

DateTime conversion with Timezone information C# [duplicate]

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....);

Categories

Resources