Convert string("2015-03-24T12:31:33.8700000") to c# datetime [duplicate] - c#

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 7 years ago.
I need to convert a string with date to valid c# date time object.
input string example = "2015-03-24T12:31:33.8700000"
output c# datetime
I tried doing this
DateTime.ParseExact(x.claimDetails.clmDOA, "yyyy-MM-dd HHmmss", CultureInfo.InvariantCulture)
Buit it gave exception
String was not recognized as a valid DateTime
Please Note: I googled thoroghly . Somehow i couldnt find any string
with "T" included as in datetime string.
Prior to downvoting if one can suggest me exactly where a question is answered with datetime string containg a T in it or of this format.yyyy-MM-dd'T'hh:mm:ss.fffffff

DateTime dt = DateTime.Parse(example);

DateTime dt = DateTime.ParseExact(example, "yyyy-MM-dd'T'hh:mm:ss.fffffff", CultureInfo.InvariantCulture);
Should work for you.

From DateTime.ParseExact documentation;
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly or an exception is thrown.
In your case, they are not.
You need to use T as a string literal delimiter, specify : as a TimeSeparator and seconds fraction (with fffffff specifier) as well.
var dt = DateTime.ParseExact("2015-03-24T12:31:33.8700000",
"yyyy-MM-dd'T'HH:mm:ss.fffffff",
CultureInfo.InvariantCulture);

DateTime.ParseExact(example , "yyyy MM dd HH:mm:ss", CultureInfo.InvariantCulture);

Related

How convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
Closed 3 years ago.
I am doing an asp.net mvc project and I need to convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second
You can use DateTime.ParseExact, here is an example :
http://net-informations.com/q/faq/stringdate.html
Finally, it should look like this :
string s = "06/22/2019 00:00:00";
DateTime myDate = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
Debug.WriteLine(myDate.ToString("MM/dd/yyyy"));
You can do this:
var dateString = "06/22/2019 00:00:00";
var datePart = dateString.Split(' ')[0];
var date = DateTime.Parse(datePart);
Though remember that DateTime will still have a default value for the time (12:00 AM), if you want the Date part only from the object, use date.Date which will return an instance with the default time (mentioned earlier).
DateTime contains default Time even if you access DateTime.Date. You can achieve format of date by converting Date into string.
Something like,
DateTime myDate = DateTime.ParseExact("06/22/2019 00:00:00", "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
string dateInFormat = $"{myDate.Year}/{myDate.Month}/{myDate.Day}";
POC : .net Fiddle
You convert the string to a DateTime object and then to display just the date portion you can use ToShortDateString like this:
var myDateTime = DateTime.Parse( "06/22/2019 00:00:00") //presumably use a variable here instead.
var date = myDateTime.ToShortDateString();
How you want to display this can be done using the CultureInfo part as shown here: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.toshortdatestring?view=netframework-4.8

How to convert date "2018-12-13T07:33:35.893Z" to 13/12/2018 [duplicate]

This question already has answers here:
How to create a .NET DateTime from ISO 8601 format
(7 answers)
Convert datetime without timezone
(4 answers)
Closed 4 years ago.
I have tried following way but doesn't work
dateTime="2018-12-13T07:33:35.893Z"
DateTime dt;
DateTime.TryParseExact(dateTime, out dt);
But I am always getting dt as {1/1/0001 12:00:00 AM}.
Can you please tell me why? and how can I convert that string to date?
I also tried Convert.ToDateTime but doesn't work.
What I actually want is getting the dd/MM/yyyy string'd DateTime so I could perform a query on a DB.
Have you got the original DateTime object or you simply have it in a string?
In case you've got it as DateTime:
string european = dateTime.ToString("dd/MM/yyyy");
In case you've got it as a string:
string date = "2018-12-13T07:33:35.893Z";
if(DateTime.TryParse(date , out DateTime result))
result.ToString("dd/MM/yyyy");
Have a look at the original MSDN documentation about the DateTime.ToString method
Since you've got a DateTime you can convert to that format:
var thisExactMoment = DateTime.Now;
thisExactMoment.ToString("dd/MM/yyyy");
With your "dateTime" variable, just perform dateTime.ToString("dd/MM/yyyy") and you're ready to go.
var dateTime = "2018-12-13T07:33:35.893Z";
var x = DateTime.Parse(dateTime).ToString(#"MM\/dd\/yyyy");

How to convert string into datetime format c# [duplicate]

This question already has answers here:
how to conver date string from one format to another format in c#?
(4 answers)
Closed 5 years ago.
I am trying to convert string 201702070001 into DateTime format like 2017-02-07 00:01
For this I tried like as below. I also followed the tutorial http://www.csharp-examples.net/string-format-datetime/. but not able to solve the problem.
string dateTime = "201702070001";
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
DateTime dt2 = DateTime.Parse(dateTime, culture, System.Globalization.DateTimeStyles.AssumeLocal);
You have to convert it using the right format. Like this:
DateTime dt2 = DateTime.ParseExact("201702070001", "yyyyMMddHHmm", CultureInfo.InvariantCulture);
You should first convert the string into DateTime object and then using the custom date formatting strings to print the DateTime, based on your needs.
Below is an example
var dateStr = "201702070001";
DateTime dt = DateTime.ParseExact(dateStr, "yyyyMMddHHmm", null);
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm"));
I am using the default FormatProvider. Depending on the locale settings on your computer, it might or might not work for you
You have provided 2 examples: 201702070001 and 20170302254. They are not consistent with each other. The first one fits yyyyMMddHHmm but the other does not. You will have to find the proper format string that you need. You can reference the correct format specifiers from Custom Date and Time Format Strings

DateTime.ParseExact() - DateTime pattern 'y' appears more than once with different values

I have spent a day trying to get DateTime.ParseExact() to work based on this correctly answered question at Parse string to DateTime in C# however, I cannot get the answer to work.
Here is my code:
string testDateRaw = #"2014-05-21 10:08:15.965";
string format = "yyyy-MM-dd H:mm:ss.yyy";
DateTime testDate = DateTime.ParseExact(testDateRaw, format, CultureInfo.InvariantCulture);
System.Console.WriteLine(testDate);
Error:
DateTime pattern 'y' appears more than once with different values.
Note: error reported in original version of the post does not show up in this sample, but may be related:
"When converting a string to DateTime, parse the string before putting each variable into the DateTime object."
Your format should be yyyy-MM-dd HH:mm:ss.fff
string testDateRaw = #"2014-05-21 10:08:15.965";
string format = "yyyy-MM-dd HH:mm:ss.fff";
DateTime testDate = DateTime.ParseExact(testDateRaw, format, CultureInfo.InvariantCulture);
System.Console.WriteLine(testDate);
See: Custom Date and Time Format Strings
The error I get with that code is the following:
DateTime pattern 'y' appears more than once with different values.
It's pretty self-explanatory. Looking at the docs, you need to use .fff here:
"yyyy-MM-dd H:mm:ss.fff"
yyy is: The year, with a minimum of three digits, but since you already have yyyy in your pattern, you get the duplicate specifier error.
Your format is wrong, you used y twice.
string testDateRaw = #"2014-05-21 10:08:15.965";
string format = "yyyy-MM-dd H:mm:ss.fff";
DateTime testDate = DateTime.ParseExact(testDateRaw, format, CultureInfo.InvariantCulture);
System.Console.WriteLine(testDate);

How to Convert string to date in c# [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 8 years ago.
i have string format of date looks like "04/16/2014 19:10", i want to convert it to DateTime.
i tried, below codes, but it didn't work. i got error like "String was not recognized as a valid DateTime."
How to convert to datetime
DateTime dt1 = DateTime.Parse(DateTimeString);
DateTime dt = System.Convert.ToDateTime(DateTimeString);
The problem is Parse, as you are using it, will take into account the current culture of the machine which means (depending on where you are) that date could be interpreted differently.
Whenever you are parsing specific dates you should use ParseExact or TryParseExact, that way you leave no room for ambiguity on how the date should be interpreted (regardless of culture)
DateTime dt;
if (DateTime.TryParseExact("04/16/2014 19:10", "MM/dd/yyyy hh:mm",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
// date was parsed correctly, use `dt`
}
You may want to use ParseExact and specify the format yourself:
DateTime d = DateTime.ParseExact("04/16/2014 19:10", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);

Categories