To change Date Format to "YYYYMMDD" [duplicate] - c#

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

Related

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

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

C# Parse DateTime from string (custom format) [duplicate]

This question already has answers here:
C# string to DateTime with timezone
(4 answers)
Closed 7 years ago.
I have this DateTime as string: 2015-08-21T10:51:25.9495986+02:00
How can I parse this string date into a DateTime object?
I usually do this:
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime _date;
DateTime.TryParseExact("2015-05-12T12:00:00", "yyyy-MM-ddTHH:mm:ss", provider, DateTimeStyles.None, out _date))
But now the end of the DateTime contains +02:00. Never faced this format and I believe this has to do something with the Time Region right?
You can simply use the o specifier for the format
DateTime.TryParseExact("2015-08-21T10:51:25.9495986+02:00", "o", provider, DateTimeStyles.None, out _date);
This will provide you a local time, to convert to universal time you can use .ToUniversalTime()
Your answer is here: C# string to DateTime with timezone
But to help:
"You should try using DateTimeOffset instead of the DateTime"
See the following example:
DateTimeOffset result = DateTimeOffset.Parse("2012-04-20 10:10:00+0200",CultureInfo.InvariantCulture);

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

Convert date yyyyMMdd to system.datetime format [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how to convert date from yyyyMMdd format to mm-dd-yyyy fomrat
I have a string which contains date in yyyyMMdd format. I want to convert that date into system date format using ConvertTo.DateTime() method or any other simple method.
string value = "19851231"; //yyyyMMdd
DateTime dateTime = 1985/12/31;
string time = "19851231";
DateTime theTime= DateTime.ParseExact(time,
"yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None);
have at look at the static methods DateTime.Parse() and DateTime.TryParse(). They will allow you to pass in your date string and a format string, and get a DateTime object in return.
http://msdn.microsoft.com/en-us/library/6fw7727c.aspx

Categories