cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar

parsing datetime field

I'm trying to parse a date+time but without success.

a = "11/9/2012 8:58:19 AM";

b = Parse Date(a, "d/m/y h:m:s"); // Returns "."

Is there a way to parse a date+time like this?

10 REPLIES 10
mpb
mpb
Level VII

Re: parsing datetime field

Not sure why you are having trouble. In 10.0.1 if I run your script with a "show()" added:

a = "11/9/2012 8:58:19 AM";

b = Parse Date(a, "d/m/y h:m:s");

show(a, b)

I get this in the Log:

a = "11/9/2012 8:58:19 AM";

b = 11Sep2012:08:58:19;

Michael

pmroz
Super User

Re: parsing datetime field

It could depend on your locale.  Are you outside of the US?

This might work better for you:

c = informat(a, "d/m/y h:m:s");

Re: parsing datetime field

I'm  indeed outside US, but I don't understand why that would effect parsing of dates in this way.

With informat(a, "d/m/y h:m:s"); the results are the same (I think that informat and parse date are equivalent?)

a = "11/9/2012 8:58:19 AM";

b = .;

c = .;

My JMP Pro version is 9.0.3

pmroz
Super User

Re: parsing datetime field

Check out this link:

You may need to replace the slashes with dashes or periods as mentioned by MS in this discussion.

Re: parsing datetime field

I think my locale the issue, it is set to 24 hour format (13:00) which makes parsing of AM/PM dates not possible...

Show(Informat("11/9/2012 8:58:19 AM", "m/d/y h:m:s"));

Show(Informat("11/9/2012 8:58:19", "m/d/y h:m:s"));

Results in:

 

Informat("11/9/2012 8:58:19 AM", "m/d/y h:m:s") = .;

Informat("11/9/2012 8:58:19", "m/d/y h:m:s") = 09Nov2012:08:58:19;

It seems there is no way to specify AM/PM in the date format.

For now I can do this ugly hack...

f = function({datetimestring},

    size = length(datetimestring);

    t = left(datetimestring, size - 3);

    d = Parse Date(t, "d/m/y h:m:s");

    If(Right(datetimestring, 2) == "PM", Date Increment(d, "Hour", 12, "actual"), d)

);

show(f("11/9/2012 8:58:19 AM"));

show(f("11/9/2012 8:58:19 PM"));

11Sep2012:08:58:19;

11Sep2012:20:58:19;

Jeff_Perkinson
Community Manager Community Manager

Re: parsing datetime field

The Parse Date() function and Informat() function are aliases of each other and neither requires a second argument.

When no second argument is supplied JMP will try to interpret the string as a date, time or datetime using a variety of formats. This will almost always work.

It certainly does in this case, regardless of the locale setting.

a = "11/9/2012 8:58:19 AM";

b = Parse Date(a);

/*:

09Nov2012:08:58:19

//:*/

a = "11/9/2012 8:58:19 PM";

b = Parse Date(a);

/*:

09Nov2012:20:58:19

-Jeff
ms
Super User (Alumni) ms
Super User (Alumni)

Re: parsing datetime field

Unfortunately there is some spurious locale dependency.

For example, this code give different results depending on locale (but time settings does not matter):

a1 = "11/9/2012 8:58:19 AM";

a2 = "11/9/2012 8:58:19";

a3 = "11-9-2012 8:58:19 AM";

a4 = "11-9-2012 8:58:19";

Show( Parse Date( a1 ), Parse Date( a2 ), Parse Date( a3 ), Parse Date( a4 ) );


//JMP 10.0.1 in OS X 10.8.2 region set to "Sweden", 24 hour time

Parse Date(a1) = .;

Parse Date(a2) = 09Nov2012:08:58:19;

Parse Date(a3) = .;

Parse Date(a4) = 09Nov2012:08:58:19;


//Same computer, but region set to "USA", 24 hour time

Parse Date(a1) = 09Nov2012:08:58:19;

Parse Date(a2) = 09Nov2012:08:58:19;

Parse Date(a3) = 09Nov2012:08:58:19;

Parse Date(a4) = 09Nov2012:08:58:19;


//Same computer, region set to "UK", 12 hour time

Parse Date(a1) = .;

Parse Date(a2) = 09Nov2012:08:58:19;

Parse Date(a3) = .;

Parse Date(a4) = 09Nov2012:08:58:19;


//Same computer, region set to "UK", 24 hour time

Parse Date(a1) = .;

Parse Date(a2) = 09Nov2012:08:58:19;

Parse Date(a3) = .;

Parse Date(a4) = 09Nov2012:08:58:19;


Re: parsing datetime field

Thanks MS, this makes clear what is going on.

It guess there is no way to specify AM/PM format as the "format" parameter in Parse Date (like %p with strptime in C)?

chrisselley
Level I

Re: parsing datetime field

Interesting

If the datetime is in the form 11SEP2012 8:58:19 AM then this works perfectly

data x;

   a="11SEP2012 8:58:19 AM";

   b=input(a,datetime24.);

   format b datetime19.;

   put b=;

run;

But if the datepart is formatted with /'s 11/9/2012 it falls apart!