46

I have a need to convert a string value in the form "YYYYMMDDHHMMSS" to a DateTime. But not sure on how, may be a DateTime.Tryparse can be used to make this happen. Or is there any other way to do it. I can do this using some string operations to take "YYYYMMDD" alone, convert to a datetime and then add HH, MM, SS separately to that DateTime. But is there any DateTime.TryParse() methods that I can use in one line to convert a "YYYYMMDDHHMMSS" format string value to a DateTime value?

SARAVAN
  • 14,571
  • 16
  • 49
  • 70
  • 1
    Exact duplicate of http://stackoverflow.com/questions/3025361/c-datetime-to-yyyymmddhhmmss-format – GalacticCowboy Jun 11 '10 at 20:23
  • 2
    Actually, it's the flipside of the original question. Almost worth a -1 for doing this. – Robaticus Jun 11 '10 at 20:24
  • I agree. But I did not realise that its much straight forward. – SARAVAN Jun 11 '10 at 20:34
  • You asked your first question a full half hour before you asked your second question. You could have modified your original question to include this instead of asking a whole new question. Frankly, the answers to your first question give you almost all the information you need to answer this question. – Robaticus Jun 11 '10 at 20:40

3 Answers3

99

Define your own parse format string to use.

string formatString = "yyyyMMddHHmmss";
string sample = "20100611221912";
DateTime dt = DateTime.ParseExact(sample,formatString,null);

In case you got a datetime having milliseconds, use the following formatString

string format = "yyyyMMddHHmmssfff"
string dateTime = "20140123205803252";
DateTime.ParseExact(dateTime ,format,CultureInfo.InvariantCulture);

Thanks

Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
  • 1
    What do you do if the date you have has the number of hours to be added or subtracted from the GMT date? An example of one of these datetimes would be "20100611221912-0500". Any advice on how to convert that would be appreciated. I don't want to make a new question cause it may be too similar to this one but at the same time, I don't know the answer to this. Thanks, Mark. – Mark May 25 '22 at 12:23
  • @Mark include time zone in the string. E.g.: yyyyMMddHHmmsszzz – Mikael Svenson May 28 '22 at 09:43
20

You have to use a custom parsing string. I also suggest to include the invariant culture to identify that this format does not relate to any culture. Plus, it will prevent a warning in some code analysis tools.

var date = DateTime.ParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101
0
class Program
{
    static void Main(string[] args)
    {

        int transactionDate = 20201010;
        int? transactionTime = 210000;

        var agreementDate = DateTime.Today;
        var previousDate = agreementDate.AddDays(-1);

        var agreementHour = 22;
        var agreementMinute = 0;
        var agreementSecond = 0;

        var startDate = new DateTime(previousDate.Year, previousDate.Month, previousDate.Day, agreementHour, agreementMinute, agreementSecond);
        var endDate = new DateTime(agreementDate.Year, agreementDate.Month, agreementDate.Day, agreementHour, agreementMinute, agreementSecond);

        DateTime selectedDate = Convert.ToDateTime(transactionDate.ToString().Substring(6, 2) + "/" + transactionDate.ToString().Substring(4, 2) + "/" + transactionDate.ToString().Substring(0, 4) + " " + string.Format("{0:00:00:00}", transactionTime));

        Console.WriteLine("Selected Date : " + selectedDate.ToString());
        Console.WriteLine("Start Date : " + startDate.ToString());
        Console.WriteLine("End Date : " + endDate.ToString());

        if (selectedDate > startDate && selectedDate <= endDate)
            Console.WriteLine("Between two dates..");
        else if (selectedDate <= startDate)
            Console.WriteLine("Less than or equal to the start date!");
        else if (selectedDate > endDate)
            Console.WriteLine("Greater than end date!");
        else
            Console.WriteLine("Out of date ranges!");
    }
}
  • Selected Date : 10.10.2020 21:00:00 Start Date : 8.10.2020 22:00:00 End Date : 9.10.2020 22:00:00 Greater than end date! – Tuncay Uyar Oct 09 '20 at 20:24
  • may you at least provide some basic description to the code? I am not really sure if this is answer or question what have you posted according to your comment – Ruli Oct 09 '20 at 20:47