3

I have a 14 number with 2 characters of AM/PM, I want to convert them to dateTime

for example: 515201483742AM

I tried this code:

DateTime result = 
DateTime.ParseExact(dateString, "Mdyyyyhmmsstt",
 CultureInfo.CreateSpecificCulture("en-US"));

the output is error in format

where 515201483742AM is 5 15 2014 8 37 42 AM

month is M with no leading 0

day also d no leading 0

hours same and tt for AM

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
M.Ghandour
  • 93
  • 2
  • 12
  • 1
    Not sure, but probably it is not possible to parse it with traditional Try/Parse methods. And also a custom parser will be really complex here. Can you ask for a less ambiguous input string? – Steve Dec 14 '14 at 10:28
  • 1
    Could you please provide the current date and time in your format? How would that look like? – Frank Dec 14 '14 at 10:34
  • 3
    I dare say it is impossible, e.g. if you had string starting with "115" there'd be no way of telling whether it's January 15th or November 5th. – Grx70 Dec 14 '14 at 11:06
  • 3
    So what time is `111201111111AM` in your format? Nov 11 1:11 AM? Jan 1 11:01 AM? If you need a compact time format, use some variant of ISO 8601 and keep the fields fixed: 20110111110100 is unambiguously 11 Jan 2011 11:01 (and 14 in length). – Jeroen Mostert Dec 14 '14 at 11:18
  • I think it cannot be done in any way, thanks guys for your help. – M.Ghandour Dec 14 '14 at 13:12

2 Answers2

2

There is no way to parse your string without changing it because M format specifier can be two digits and it will map to parse 51 as a month. That's why your operation throws FormatException. Same with h format specifier.

That's why we need to change your string from 515201483742AM to 05152014083742AM to provide M format specifier will map 05 and h format specifier will map 08.

You can use a method like1;

public static DateTime? ParseDate_Mdyyyyhmmsstt(string date)
{
    if (date == null)
        return null;
    if (date.Length < 14)
        return null;
    if (date.Length == 14)
        date = date.Insert(0, "0").Insert(8, "0");
    DateTime dt;
    if (DateTime.TryParseExact(date, "Mdyyyyhmmsstt",
                               CultureInfo.InvariantCulture,
                               DateTimeStyles.None, out dt))
        return dt;
    return null;
}

And call it like;

var dt = ParseDate_Mdyyyyhmmsstt("515201483742AM");
Console.WriteLine(dt.Value); // 15.05.2014 08:37:42

1: I know this is not an elegant solution but it works in your case when your month and hour part is 1 digit.

Read: DateTime conversion from string C#

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    Sure, it works for this particular input, but for input `"1215201483742AM"` (which is 15.12.2014 08:37:52) it will return `null`... – Grx70 Dec 14 '14 at 11:11
  • @Grx70 Of course it will return `null` because `"1215201483742AM".Length` is `15`. That's why I said, **in your case**. For your example, OP can be skip `date.Insert(0, "0")` part because month part of your string is `2` digit but he still have to add `Insert(8, "0")` part because hour part is still `1` digit. – Soner Gönül Dec 14 '14 at 11:13
  • You're right, but given input of length 15 how can you tell whether you should skip `date.Insert(0, "0")` or `.Insert(8, "0")`? – Grx70 Dec 14 '14 at 11:23
  • @Grx70 I can't. No one can. OP is responsible to know what format of input will be in parsing operation, not me. I suggested to him as an answer specific format when month and hour part is 1 digit. – Soner Gönül Dec 14 '14 at 11:32
  • it is not fixed it there are many: these are some random values `1272014105547AM`- `3252014113727AM` - `526201493920PM` - `9112014110901AM` – M.Ghandour Dec 14 '14 at 12:58
  • Also like Jeroen and Grx70 said up in the comments how it can be solved? – M.Ghandour Dec 14 '14 at 13:09
  • I think it cannot be done in any way, thanks guys for your help. – M.Ghandour Dec 14 '14 at 13:11
  • @M.Ghandour Yes, based your examples, it seems can not be done :( – Soner Gönül Dec 17 '14 at 15:56
0

use double "MM" and "dd" Instead of single "M" and "d"

DateTime result = DateTime.ParseExact(dateString,"MMddyyyyhmmsstt",CultureInfo.CreateSpecificCulture("en-US"));

it will work.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93