简体   繁体   中英

converting a string to a DateTime format

I'm trying to parse 09/01/2015 00:00:00 to the format yyyy-MM-ddThh:mm:ssZ using following method:

DateTime.ParseExact("09/01/2015 00:00:00", "yyyy-MM-ddThh:mm:ssZ", (IFormatProvider)CultureInfo.InvariantCulture);

But I'm getting String was not recognized as a valid DateTime

Can anyone tell me why? I believe 09/01/2015 00:00:00 is a valid DateTime format?

From DateTime.ParseExact

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.

I assume your 09 part is day numbers, you can use dd/MM/yyyy HH:mm:ss format instead.

var dt = DateTime.ParseExact("09/01/2015 00:00:00", 
                             "dd/MM/yyyy HH:mm:ss", 
                             CultureInfo.InvariantCulture);

Since CultureInfo already implements IFormatProvider , you don't need to explicitly cast it.

I don't understand this. So it means I first have to correct my string and secondly I can do a ParseExact(). I thought ParseExact could handle the given string...

ParseExact is not a magical method that can parse any formatted string you suplied. It can handle only if your string and format perfectly matches based on culture settings you used.

Try this code:

var text = "09/01/2015 00:00:00";
var format = "dd/MM/yyyy HH:mm:ss";
var dt = DateTime.ParseExact(text, format, (IFormatProvider)CultureInfo.InvariantCulture);

You'll notice that the format must structurally match the text you're trying to parse exactly - hence the ParseExact name for the method.

格式不匹配,您需要将09/01/2015更改为2015-01-09或将yyyy-MM-dd部分更改为dd/MM/yyyy

The ParseExact-method is no ultimate method that converts ANY dateformat into another one, it is simply to parse a given string into a datetime using the provided format. Thus if your inout does not match this format the method will throw that exception.

As a datetime is internally only a number there is no need to convert one format into another at all, so as long as you know your input-format you can build a date from it which has nothing to do with any formatting which you may need when you want to print that date to your output. In this case you WILL need a formatter.

As most people have stated the error is coming from the fact that the date in string format doesn't match the format you are saying it's in. You are saying that 09/01/2015 00:00:00 is in the format "yyyy-MM-ddThh:mm:ssZ", which it's not, hence the error. To rectify this you need to either alter the format the string is in, or more likely, change the format you are saying the date is in. So change "yyyy-MM-ddThh:mm:ssZ" to "dd/MM/yyyy HH:mm:ss".

In a more long term view how are you arriving at that date? Is it possible that the format may change (input but the user)? If so it might be better to try and avoid the error being thrown and handle it better with TryParseExact . To make use of this best I generally output a nullable DateTime and then check if it's null. If you don't do this then if the parse fails it will simply make the output datetime the minimum value.

Something like this should work:

public DateTime? StringToDate (string dateString, string dateFormat)
{
    DateTime? dt;
    DateTime.TryParseExact(dateString, dateFormat, null, System.Globalization.DateTimeStyles.None, out dt);
    return dt;
}

Then you can use it like this:

DateTime? MyDateTime = StringToDate("09/01/2015 00:00:00", "dd/MM/yyyy HH:mm:ss");

if(MyDateTime != null)
{
    //do something
}

另一种简单的方法......

var dt = Convert.ToDateTime(Convert.ToDateTime("09/01/2015 00:00:00").ToString("yyyy-MM-ddThh:mm:ssZ"))

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