This question already has an answer here:
DateTime.ParseExact, Ignore the timezone
(1 answer)
Closed 4 years ago.
I have been searching to convert string to datetime with ignoring the offset.
I have a string with local date time as 2017-02-13T12:11:03.303 +01:00 i want to ignore the offset part and string should be converted to 2017-02-13 12:11:03.303 as datetime format. I searched google but could not find one.
Referred How to convert string to DateTime in C#?
Not a duplicate of DateTime.ParseExact, Ignore the timezone as while searching didn't know its realted to DateTimeOffset.Searched using layman terms and no proper result found.
Use the DateTimeOffset.Parse method:
string s = "2017-02-13T12:11:03.303 +01:00";
DateTime dt = DateTimeOffset.Parse(s, null).DateTime;
Related
This question already has answers here:
How to create a .NET DateTime from ISO 8601 format
(7 answers)
Closed 4 years ago.
I have to parse following string to DateTime object:
2018-03-27T14:33:54.4092769+03:00
I know that there are few methods that can parse string, for example DateTime.Parse() and DateTime.ParseExact(). I checked first method but there is no specification for this exact pattern.
So how can I parse this format to DateTime object?
That ISO 8601-format is already supported by DateTime.Parse/TryParse:
bool valid = DateTime.TryParse(strDate, out DateTime dt); // true
This question already has answers here:
Parse string to DateTime in C#
(9 answers)
DateTime.Now.ToString("MM/DD/YYYY") contains incorrect month
(2 answers)
Closed 4 years ago.
How to convert string MM/dd/yyyy HH:ss to DateTime without changing the format. I just need to change the datatype from string to DateTime that's it. I have tried various ways but it doesn't give me my expected output.
String Input : 01/28/2018 20:00
Expected DateTime output : 01/28/2018 20:00 (only datatype should change)
Below are the some methods that I have tried:
DateTime.ParseExact("01/28/2018 20:00","MM/dd/yyyy HH:ss", CultureInfo.InvariantCulture): gives me 2018-01-28T20:00:00
Convert.DateTime("01/28/2018 20:00") gives me "String was not recognized as a valid DateTime" exception
DateTime.Parse("01/28/2018 20:00") this also gives me the same exception
Any help much appreciated.
As it was told in comments above, DateTime itself has no formatting, it happens only for string representation of the date and time. So, the best thing you can do here is to store two values: DateTime value itself and expected string format. Then, once converting your date & time into printable thing you just have to specify that stored format.
This question already has answers here:
C# DateTime to "YYYYMMDDHHMMSS" format
(18 answers)
Closed 5 years ago.
i wanted to convert 26/Jun/2016 at 13:14 from a string to datetime data type
for saving to sql
string DtTime = ds.Tables["VOUCHER"].Rows[0]["BASICDATETIMEOFINVOICE"].ToString();
how to split as a string
You need to escape the / and the at in the format string, then you can use ParseExact:
DateTime.ParseExact("26/Jun/2016 at 13:14", "dd/MMM/yyyy 'at' HH:mm", CultureInfo.InvariantCulture);
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
But are you sure that the value is not stored as DateTime? Check by using ds.Tables["VOUCHER"].Rows[0].Field<DateTime>("BASICDATETIMEOFINVOICE"). Then no conversion from Object to string to DateTime is needed. If not you should consider to store it as DateTime in the first place, wherever the DataTable was filled from.
This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 5 years ago.
I'm writing an automation date and I want to extract a string, turn it into a DateTime object, and compare it with the current date and time.
The string in question has this format: 7/28/2017 1:17:29 PM
How can I convert it to a DateTime object to compare with the current time (Basically, my end goal is to verify that it is within a few minutes of the current time)
Use DateTime.Parse() or DateTime.TryParse()
System.Convert can convert to and from many types. For example...
int intElapsedMinutes = (DateTime.Now - Convert.ToDateTime("7/28/2017 1:17:29 PM")).TotalMinutes
This question already has answers here:
Convert UTC/GMT time to local time
(12 answers)
Closed 6 years ago.
I'm using an api that provides time as a string, for example "14:45". Ultimately, I would like to convert that UTC time string to the user's local time zone, but I'm not exactly sure how to do this.
The only solution I could think of is to convert the string to a DateTime and then convert that DateTime to local before pushing that out as a string back in the original format (ex. "14:45" becomes "11:45"). I'm stuck trying to convert the string to a DateTime given that it's not in the typical format. Any suggestions? Thanks!
Alright I was able to figure it out using the following:
string time = "08:00";
var convertedTime = Convert.ToDateTime(time).ToLocalTime().TimeOfDay.ToString().Substring(0,5);