简体   繁体   中英

Parse string value to datetime value

I need to parse string value to date time value, I have date in this format:

DD.MM.YYYY

I want to parse value in this format:

YYYY-MM-DD

I tried to do it like this:

DateTime.ParseExact(date_req, "yyyy-MM-dd", CultureInfo.InvariantCulture); 

But i have an error: String was not recognized as a valid DateTime.

Is there a way to do this?

If you have a string in the format DD.MM.YYYY why are you passing YYYY-MM-DD to your ParseExact function?

Try like this:

string dateStr = "12.06.2012";
DateTime date = DateTime.ParseExact(dateStr, "dd.MM.yyyy", CultureInfo.InvariantCulture);

Then when you want to output this DateTime instance somewhere you could use the YYYY-MM-DD format, like this:

string result = date.ToString("yyyy-MM-dd");

When parsing a date you need to specify the format you want to read , not the format you want as output later.

So use dd.MM.yyyy as argument to ParseExact .

Check DateTime.ParseExact Method (String, String, IFormatProvider) 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.

you have to specify format string as DD.MM.YYYY rather than "yyyy-MM-dd" .

try this:

DateTime dateValue = DateTime.ParseExact(date_req, "DD.MM.YYYY", CultureInfo.InvariantCulture );

// use this when you need to show that formatted date value
string formattedDate = dateValue.ToString("yyyy-MM-dd");

Better way is that use DateTime.TryParseExact Method , if you want it as date rather than string modify your culture info and date separator.

CultureInfo enUS = new CultureInfo("en-US"); 
string dateString;
DateTime dateValue;
dateString = "05.01.2009"; 
if (DateTime.TryParseExact(dateString, "DD.MM.YYYY", enUS, 
                        DateTimeStyles.None, out dateValue))
   Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                     dateValue.Kind);
else
   Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

I think what you want to do is parse your dd.MM.yyyy and then display it as yyyy-MM-dd .

You first have to parse the string into a DateTime :

DateTime date = DateTime.ParseExact(date_req, "yyyy-MM-dd", CultureInfo.InvariantCulture); 

Now date is a representation of the date that the computer actually understands (before it was just a string). You can now display this object anyway you want:

string yyyyMMdd = date.ToString("yyyy-MM-dd");
string arabic = date.ToString("yyyy-MM-dd", new CultureInfo("ar"));
// and so on

Don't forget that when converting dates from strings to DateTime and back, culture and time zones are worth keeping in mind.

First parse it in the existing format then convert to the string format you want.

var date = DateTime.ParseExact(date_req, "dd.MM.yyyy", CultureInfo.InvariantCulture); 
var str = date.ToString("yyyy-MM-dd");

You can first convert it to a character array. then you can parse day/month/year separately to integers . You know the indexes of the numbers so this will be easy. after that you can concatenate every element in the way you like.

Error. You have a cake and you want to eat a stake. In order to convince your stomach that that the cake is a stake you have to transform the cake to a stake. This cannot be done. Parsing is about accepting a value as it comes and use a pattern (or more) to translate it to something else and not transform it. So what you want may be right but you ask is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM