Convert string with date & UTC offset to DateTime [duplicate] - c#

This question already has answers here:
Convert UTC/GMT time to local time
(12 answers)
Closed 6 years ago.
I am trying to parse a string "20160918000500 +0200" to DateTime containing offset value "+0200".
I tried the following but it gives invalid DateTime exception.
DateTime dtDateTime = DateTime.Parse("20160918000500 +0200",new CultureInfo("yyyyMMddHHmmss zzz"));
Is there a way to convert the String exactly to Datetime with UTC offset value?

To preserve your Offset, use the DateTimeOffset.ParseExact method:
string str = "20160918000500 +0200";
var result = DateTimeOffset.ParseExact(str, "yyyyMMddHHmmss zzz", CultureInfo.InvariantCulture);
Console.WriteLine(result);

I would suggest to try out one of ParseExact methods of DateTime class

Related

How to convert date "2018-12-13T07:33:35.893Z" to 13/12/2018 [duplicate]

This question already has answers here:
How to create a .NET DateTime from ISO 8601 format
(7 answers)
Convert datetime without timezone
(4 answers)
Closed 4 years ago.
I have tried following way but doesn't work
dateTime="2018-12-13T07:33:35.893Z"
DateTime dt;
DateTime.TryParseExact(dateTime, out dt);
But I am always getting dt as {1/1/0001 12:00:00 AM}.
Can you please tell me why? and how can I convert that string to date?
I also tried Convert.ToDateTime but doesn't work.
What I actually want is getting the dd/MM/yyyy string'd DateTime so I could perform a query on a DB.
Have you got the original DateTime object or you simply have it in a string?
In case you've got it as DateTime:
string european = dateTime.ToString("dd/MM/yyyy");
In case you've got it as a string:
string date = "2018-12-13T07:33:35.893Z";
if(DateTime.TryParse(date , out DateTime result))
result.ToString("dd/MM/yyyy");
Have a look at the original MSDN documentation about the DateTime.ToString method
Since you've got a DateTime you can convert to that format:
var thisExactMoment = DateTime.Now;
thisExactMoment.ToString("dd/MM/yyyy");
With your "dateTime" variable, just perform dateTime.ToString("dd/MM/yyyy") and you're ready to go.
var dateTime = "2018-12-13T07:33:35.893Z";
var x = DateTime.Parse(dateTime).ToString(#"MM\/dd\/yyyy");

Get original DateTime offset [duplicate]

This question already has answers here:
Converting string to DateTime with offset
(5 answers)
Closed 4 years ago.
I have following code
string dateString = "2018-04-20T12:22:32.8526432-05:30";
var objDate = DateTime.Parse(dateString);
string newDateString = objDate.ToString(); //"4/20/2018 1:52:32 PM"
Once string is parsed to DateTime how do I get original DateTime offset i.e. -5:30 from objDate? I tried following code but it gives local offset i.e. -4:00 but not -5:30.
var offset = TimeZone.CurrentTimeZone.GetUtcOffset(objDate);
Please note that I want to get offset from DateTime object (objDate) and not from string variable dateString.
As mentioned by #Evk the DateTime type does not store the offset. Instead as suggested by #maccettura
string dateString = "2018-04-20T12:22:32.8526432-05:30";
var offset = DateTimeOffset.Parse(dateString);
// returns -05:30:00
Console.WriteLine(offset.Offset);

How to convert string to datetime that with million seconds? [duplicate]

This question already has answers here:
Converting a string to DateTime object
(5 answers)
Closed 7 years ago.
I have a string which value is "2016-01-07 20:43:01,803".
I'd like to convert it use DateTime.Parse method. it is failed.
How to convert to datetime with this type of string?
You could use the ParseExact method.
var input = "2016-01-07 20:43:01,803";
DateTime dt = DateTime.ParseExact(input, "yyyy-MM-dd HH:mm:ss,fff", CultureInfo.InvariantCulture);
Try to use DateTime.ParseExact with the right format. (the last phrase is very very important: right format)
DateTime dt = DateTime.ParseExact("2016-01-07 20:43:01,803", "yyyy-MM-dd HH:mm:ss,fff", null);

C# Parse DateTime from string (custom format) [duplicate]

This question already has answers here:
C# string to DateTime with timezone
(4 answers)
Closed 7 years ago.
I have this DateTime as string: 2015-08-21T10:51:25.9495986+02:00
How can I parse this string date into a DateTime object?
I usually do this:
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime _date;
DateTime.TryParseExact("2015-05-12T12:00:00", "yyyy-MM-ddTHH:mm:ss", provider, DateTimeStyles.None, out _date))
But now the end of the DateTime contains +02:00. Never faced this format and I believe this has to do something with the Time Region right?
You can simply use the o specifier for the format
DateTime.TryParseExact("2015-08-21T10:51:25.9495986+02:00", "o", provider, DateTimeStyles.None, out _date);
This will provide you a local time, to convert to universal time you can use .ToUniversalTime()
Your answer is here: C# string to DateTime with timezone
But to help:
"You should try using DateTimeOffset instead of the DateTime"
See the following example:
DateTimeOffset result = DateTimeOffset.Parse("2012-04-20 10:10:00+0200",CultureInfo.InvariantCulture);

Convert date yyyyMMdd to system.datetime format [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how to convert date from yyyyMMdd format to mm-dd-yyyy fomrat
I have a string which contains date in yyyyMMdd format. I want to convert that date into system date format using ConvertTo.DateTime() method or any other simple method.
string value = "19851231"; //yyyyMMdd
DateTime dateTime = 1985/12/31;
string time = "19851231";
DateTime theTime= DateTime.ParseExact(time,
"yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None);
have at look at the static methods DateTime.Parse() and DateTime.TryParse(). They will allow you to pass in your date string and a format string, and get a DateTime object in return.
http://msdn.microsoft.com/en-us/library/6fw7727c.aspx

Categories