Convert String to DateTime Exactly Same format [duplicate] - c#

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.

Related

C# DATE TIME CONVERSIONS WITH TIME ZONES [duplicate]

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.

How to ignore offset from string datetime [duplicate]

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;

Tryparse datetime string to datetime format [duplicate]

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.

Convert a formatted date/time string into a DateTime object for comparison? [duplicate]

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

How to parse string in the format "20150129163809" to datetime? [duplicate]

This question already has answers here:
Convert String value format of YYYYMMDDHHMMSS to C# DateTime
(3 answers)
Closed 7 years ago.
I my application I need to convert a string in the datetime format as below:
var k= DateTime.Parse("20150129163809");
But its throwing up an error message that "String was not recognized as a valid DateTime."
But, when I do
DateTime.Parse("2015-01-29 16:38:09")
its working fine...
What could be wrong?
Use ParseExact to parse well-formatted strings:
var k = DateTime.ParseExact("20150129163809","yyyyMMddHHmmss",CultureInfo.InvariantCulture);
DateTime.Parse is fairly limited in its capabilities and does not try and "guess" what format the string is in.

Categories