简体   繁体   中英

Use DateTime.TryParseExact to format date string and ignore time

I want to parse string date to DateTime but ignoring time. My expected date format is M/d/yyyy which is 3/29/2018 (without leading zero). The thing is string can be with or without time part and time can have different formats that I will not predict.

var inputDateString = "12/31/2017 12:00:00 AM" // false, but I want to parse
var inputDateString = "12/31/2017" // true
DateTime.TryParseExact(inputDateString, "M/d/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsedDate); 

Is there any way to parse date string having only specific date format and ignore time?

There is an overload to TryParseExact that allows you to pass in multiple formats. If you know in advance which formats to expect, you can use this overload:

void Main()
{
    string[] validFormats = {"M/d/yyyy", "M/d/yyyy hh:mm:ss tt"};

    var inputDateString1 = "12/31/2017 12:00:00 AM"; // false, but I want to parse
    var inputDateString2 = "12/31/2017"; // true

    DateTime.TryParseExact(inputDateString1, validFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt1);
    DateTime.TryParseExact(inputDateString2, validFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt2);

}

You can then get only the date portion using the Date property.

You could strip the time part from the input string, or parse the full input, using only the .Date part.

var parsedDate = DateTime.MinValue;
var inputDateString = "12/31/2017 12:00:00 AM"; // false, but I want to parse

// option 1: use only the date part
if (DateTime.TryParseExact((inputDateString ?? "").Split(' ')[0] , "M/d/yyyy", 
        CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate))
    Console.WriteLine(parsedDate);

// option 2: use the full input, but ignore the time
if (DateTime.TryParseExact(inputDateString, "M/d/yyyy hh:mm:ss tt", 
        CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate))
    Console.WriteLine(parsedDate.Date);

Personally, I would go with the first option.

If you always want to only parse the Date portion that can be done by explicitly ensuring the string is only 10 characters in length. This is a somewhat convoluted example but you can strip out what you don't need, you'll get the idea:

var inputDateString = "12/31/2017 12:00:00 AM";
string datePortion = string.Empty;
DateTime dt;

if (inputDateString.Length>10)
{
    // take first 10 characters of inputDateString
    datePortion = inputDateString.Substring(0, Math.Min(inputDateString.Length, 10));
}
else if (inputDateString.Length==10)
{
    // inputDateString is already 10 characters
    datePortion = inputDateString;
}
else
{
    // inputDateString is less than 10 characters, no date found, do nothing.
}

if(!DateTime.TryParse(datePortion, out dt))
{
   // handle error that occurred, 
}
else
{
    // parse was successful, carry on.
}

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