Converting string with month that is not integer to datetime [duplicate] - c#

This question already has answers here:
datetime.parse and making it work with a specific format
(2 answers)
Closed 9 years ago.
I have strings in the format
Mar 25 2013 6:30PM
and I want to convert them to datetime objects in the format
2013-03-25 18:30:00
how would I go about doing this?

Try this
DateTime dt
CultureInfo provider = CultureInfo.InvariantCulture;
System.Globalization.DateTimeStyles style = DateTimeStyles.None;
DateTime.TryParseExact("Mar 25 2013 6:30PM", "MMM d yyyy h:mtt", provider, style, out dt);

you can use:
var date = DateTime.Parse("Mar 25 2013 6:30PM");
Console.WriteLine(date.ToString()); /*This will output the date in the required format */

Related

Convert Oct 27 2022 9:09:50:693PM into c# dateTime [duplicate]

This question already has answers here:
Parse a string containing date and time in a custom format
(3 answers)
Closed 2 years ago.
How to convert below DateTime into C# (mm/dd/yy hh:mm:ss)
Oct 27 2022 9:09:50:693PM
Error string was not in recognized format
Tried with below code:
var cultureInfo = new CultureInfo("en-US", true);
DateTime LastUpdateDate = DateTime.Parse("Oct 27 2022 9:09:50:693PM", cultureInfo, DateTimeStyles.NoCurrentDateDefault);
Use DateTime.ParseExact() with "MMM dd yyyy h:mm:ss:ffftt" format,
using System;
using System.Globalization;
...
var cultureInfo = new CultureInfo("en-US", true);
DateTime LastUpdateDate = DateTime.ParseExact("Oct 27 2022 9:09:50:693PM", "MMM dd yyyy h:mm:ss:ffftt", cultureInfo);
Console.WriteLine(LastUpdateDate.ToString());
.Net Fiddle
Note: You have two spaces between year and time. No space between milliseconds and AM/PM designator.
Please check your string date and time and update format(Spaces in DateTime format) accordingly
MSDN documentation for Custom date and time format strings

To change Date Format to "YYYYMMDD" [duplicate]

This question already has answers here:
Change format of DateTime object in c# and store back as DateTime object
(1 answer)
Set DateTime format
(6 answers)
Closed 3 years ago.
I have DateTime which contains standard Date Time. I am trying to convert this into "yyyymmdd" format.
DateTime deliveryDate = myRepository.DeliveryDate; //3/31/2018 12:00:00 AM;
DateTime myDeliveryDate = DateTime.ParseExact(deliveryDate.ToString(),
"yyyymmdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None)
What is wrong in the above code, I got an error like this:
String was not recognized as a valid DateTime.at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
The DateTime class doesn't store the date in a particular format, if you want it represented like yyyymmdd you have to convert it to a string:
string myDeliveryDate = deliveryDate.ToString("yyyyMMdd");
You should note that month is a capital M (small m is for minutes).
DateTime is just the container of all the information. If you want it parsed in yyyyMMdd format, use
string myDeliveryDate = deliveryDate.ToString("yyyyMMdd");

String was not recognized as a valid DateTime format dd/MM/yyyy [duplicate]

This question already has answers here:
DateTime ParseExact string was not recognize as a DateTime C#
(2 answers)
Closed 5 years ago.
string d = "4/2/2018 12:00:00 AM";// 2nd April 2018
DateTime startD;
startD = DateTime.ParseExact(d, "MM/dd/yyyy", CultureInfo.InvariantCulture);
How can I get the string recognized as a valid DateTime?
Its called ParseExact for a reason.
Use "M/d/yyyy hh:mm:ss tt"
See https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings for the custom date time specifiers and DateTime.ParseExact
Alternatively you could swith to DateTime.TryParse(...) Syntax which I would prefere unless you are 100% certain what your input is.
As Patrick Artner noted, your pattern does not match your format.
The correct format is "M/d/yyyy hh:mm:ss tt" with
M ... Represents the month as a number from 1 through 12.
d ... Represents the day of the month as a number from 1 through 31
string d = "4/2/2018 12:00:00 AM";// 2nd April 2018
DateTime startD;
startD = Convert.ToDateTime(d);

How to convert string to datetime that with million seconds? [duplicate]

This question already has answers here:
Converting a string to DateTime object
(5 answers)
Closed 7 years ago.
I have a string which value is "2016-01-07 20:43:01,803".
I'd like to convert it use DateTime.Parse method. it is failed.
How to convert to datetime with this type of string?
You could use the ParseExact method.
var input = "2016-01-07 20:43:01,803";
DateTime dt = DateTime.ParseExact(input, "yyyy-MM-dd HH:mm:ss,fff", CultureInfo.InvariantCulture);
Try to use DateTime.ParseExact with the right format. (the last phrase is very very important: right format)
DateTime dt = DateTime.ParseExact("2016-01-07 20:43:01,803", "yyyy-MM-dd HH:mm:ss,fff", null);

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

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

Categories