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

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.

Related

C# - Parse Process.StartTime to a certain format [duplicate]

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"));

C# convert roundtrip date string to DateTime object [duplicate]

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

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.

c# - how to convert 26/Jun/2016 at 13:14 to 2016-01-26 13:14:00 [duplicate]

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.

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.

Categories