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
Related
This question already has answers here:
C# DateTime to "YYYYMMDDHHMMSS" format
(18 answers)
DateTime ParseExact string was not recognize as a DateTime C#
(2 answers)
Closed 4 months ago.
I can't figure a way to parse a Datetime object that is "10/24/2022 4:01:35 PM" to "24/10/2022 16:01:35".
Can't find an answer for this exact format.
Thanks in advance!
I guess you don't need to parse that string to DateTime since you have it already in Process.StartTime and you're looking at it in the debugger. Then use it directly. If you really have it as string you simply need to use DateTime.Parse with InvariantCulture:
DateTime dt = DateTime.Parse("10/24/2022 4:01:35 PM", System.Globalization.CultureInfo.InvariantCulture);
and for the output:
Console.Write(dt.ToString("dd'/'MM'/'yyyy HH:mm:ss"));
This question already has answers here:
Given a DateTime object, how do I get an ISO 8601 date in string format?
(18 answers)
Closed 6 months ago.
Im consuming a REST api that returns a datetime in the format 2022-09-08T00:21:32.712+03:00 this translates to 08/09/2022 00:21. How do I convert a date value say 08/09/2022 00:21 to the format 2022-09-08T00:21:32.712+03:00 in C#? I have tried below cord without success
string NIRA_CREATEDDATE_FORMATED = NIRA_CREATEDDATE.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'");
That is an ISO 8601 format. C# will output it if you use the "o" format.
DateTime.Now.ToString("o")
Note that you'll likely see a higher level of precision than the one you've provided, but that won't typically matter.
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:
Converting a String to DateTime
(17 answers)
Closed 6 years ago.
How can convert this string yy-MM-dd HH:mm:ss.SSS to datetime in c#?
You can use the DateTime.ParseExact() method and define the exact format string that you are expecting to receive :
// This will parse a DateTime object using the "yy-MM-dd HH:mm:ss.fff" format
var date = DateTime.ParseExact(input,"yy-MM-dd HH:mm:ss.fff", null);
This assumes that the Y characters will represent a two-digit year and the S characters you were looking for would be considered fractional seconds, which have been converted to the y and f formats respectively.
Example
You can see a working example of this here.