Tryparse datetime string to datetime format [duplicate] - c#

This question already has answers here:
Converting a string to a DateTime with more than 7 decimals of milliseconds
(2 answers)
Closed 5 years ago.
I am trying to parse "31.01.2017 07:56:29.470000000"date time string into datetime format.
Code used:
DateTime requiredDate;
string date = "31.01.2017 07:56:29.470000000";
DateTime.TryParseExact(date,
"dd.MM.yyyy hh:mm:ss.fffffff",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out requiredDate);
NOTE: date string is exact "31.01.2017 07:56:29.470000000", however if I use "31.01.2017 07:56:29.4700000" then it is working fine.
Please parse "31.01.2017 07:56:29.470000000".

The problem lies in the maximum allowed number of fs in your parse string: The maximum value is fffffff (7 fractions). Your string contains 9 of them.
You can find this limitation in the documentation. It mentions all possible values between f and fffffff, but not further.

The problem is in the number of f which as a maximum of 7. You are using the Round-trip date/time pattern that complies with ISO 8601. Please see the documentation.
The "O" or "o" standard format specifier corresponds to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string for DateTime values and to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz" custom format string for DateTimeOffset values.
As you can see, there are only 7 digits of f in the format indicated by the documentation.
To solve your problem you should remove the last 2 digit from your input.

Related

What is this Date Time Format? 2019-01-17T10:49:55-05:00 [duplicate]

This question already has answers here:
What is this date format? 2011-08-12T20:17:46.384Z
(11 answers)
Convert datetime string with this format: (yyyy-MM-dd'T'hh:mm:ss-zzz)
(2 answers)
Closed 4 years ago.
As the question states, I'm trying to figure out what this datetime format is: 2019-01-17T10:49:55-05:00. Through research, I've figured out up to this point: yyyy-MM-ddTHH:mm:ss But the last few numbers, I have no idea what they represent. The only thing I came up with is milliseconds but that is usually 3 number after a decimal like this: .fff which does not fit this scenario.
It looks like ISO8601 format. The last section is how far from UTC/GMT it is. In this case -5 hours.
To add a bit more, this is THE standard way to transfer date and times between systems.
It is a date in ISO8601 format. The -05:00 represents the UTC offset.
You can parse it using DateTime.ParseExact like so:
var ds = "2019-01-17T10:49:55-05:00";
var date1 = DateTime.ParseExact(ds, "yyyy-MM-ddTHH:mm:sszzz", CultureInfo.InvariantCulture);
Its an ISO 8601 timestamp with timezone information. The -05:00 means minus 5 hours.

Convert String to DateTime Exactly Same format [duplicate]

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.

String yy-MM-dd HH:mm:ss.SSS convert to datetime in C# [duplicate]

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.

Generate date string with ToString("u") and with a T before the hour [duplicate]

This question already has answers here:
date format yyyy-MM-ddTHH:mm:ssZ
(10 answers)
Closed 7 years ago.
I'm developing a C# library with .NET Framework 4.5.
I need to generate this date string: 2015-12-01T07:54:20Z
But now I'm generating this date string: 2015-12-01 07:54:20Z (the same that previous one but with out the T).
To generate that date I'm using this code: DateTime.Now.ToUniversalTime().ToString("u")
How can I generate the other date string with the T?
Your format (without the trailing Z) is called the sortable format, and it has the pre-defined format specifier "s":
DateTime.UtcNow.ToString("s") + "Z"
yields
2015-12-18T09:04:58Z
You can write whole format DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
or use less elegant solution ToString("u").Replace(" ","T")
HH is for 24h format and hh is for 12h format
Either
DateTime.UtcNow.ToString("yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
give you 2015-12-18T13:57:31.2311892-04:00
or
DateTime.UtcNow.ToString("o");
give you 2015-12-18T14:01:54.9571247Z

How to convert DateTime object to dd/mm/yyyy in C#? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
Convert a string to a date in .net
format date in c#
What markup is used to format StackOverflow questions?
How to convert DateTime object to dd/mm/yyyy in C#?
One thing to note in addition to the other answers - / is a format character itself, representing the local date separator. If you want to make absolutely sure it uses an actual slash, either use the invariant culture (which uses a slash):
string s = dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
or escape the slashes:
string s = dateTime.ToString("dd'/'MM'/'yyyy");
Are you talking about converting to a string for printing or something?
String s = DateTime.ToString("dd/MM/yyyy");
And to be complete, here is more information about DateTime.ToString and DateTime formatting in general.
DateTime d = DateTime.Now;
string s = d.ToString("dd/MM/yyyy");
Console.WriteLine(s);

Categories