2

I want to convert a time string say '12:05 PM' into a datetime using Date.Parse in Javascript. When I pass in a value of say 12:05 PM or 12:10 PM or ... or 12:55 PM the value returned by startTime below is null, i.e. startTime = null

But when I pass in values of 1:00 PM, 1:05 PM, 1:10 PM, 12:00 AM,...,12:00 PM it works fine
returning me a Date with the time included.

This is the code line causing an issue:

var startTime = Date.parse($("#<%= StartTime.ClientID %>").val());  //code causing the issue
    

And StartTime is a textbox.

I am writing the above code in client/html in an ASP.NET application on the web form.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ruruboy
  • 626
  • 3
  • 12
  • 36

4 Answers4

3

If you're using date.js then try (as per test case here)

Date.parseExact("12:05 PM", "hh:mm tt");

This should also pick up if you've loaded the library correctly.

jhsowter
  • 619
  • 3
  • 8
  • Assuming my date string is "12:05 PM" what is the parameter format being passed into the Date.parseExtract please. – Ruruboy Mar 16 '12 at 00:16
2

It works fine here:

http://jsfiddle.net/vuURb/396/

It's possible that it is a library loading issue, but you claim it works on some times and not others. Have you tried outputting the value of the text box to the console before feeding it to Date.parse()?

Jeff B
  • 29,943
  • 7
  • 61
  • 90
  • In QuickWatch when debugging I get the value of $("#<%= StartTime.ClientID %>").val() = "12:05 PM". But when I put it into Date.parse it is null. In the same quickwatch window I put the date "1:05 PM" into the Date.parse method and that works fine. I think Date.parse does not like 12's in increment when they are PM. – Ruruboy Mar 16 '12 at 00:15
  • The CDN was no longer valid, so the script was defaulting to the standard Date.parse(). I fixed the jsfiddle, and it should work now. – Jeff B Feb 21 '18 at 19:59
1

there's a good utility for dates named date.js.

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
  • doesn't it work? I mean you can add a dummy date before the time, and then parse it and just use the time part? – Taha Paksu Mar 15 '12 at 23:17
1

Based on this answer you can do this:

var startTime = new Date();
var time = $("#<%= StartTime.ClientID %>").val().match(/(\d+)(?::(\d\d))?\s*(p?)/);
startTime.setHours(parseInt(time[1]) + (time[3] ? 12 : 0) );
startTime.setMinutes( parseInt(time[2]) || 0 );

I just read on your reply to the other question that you are using date.js. If you really are using it, your code is correct, then the problem should be that you are not loading the library properly, and you are using the native Date object.

Community
  • 1
  • 1
davoclavo
  • 1,432
  • 16
  • 19
  • Hi Thirit. My time has a range from 12:00 AM, 12:05 AM, 1:00 AM, 1:05 AM, 1:10 AM...12:00 PM, 12:05 PM, 12:10 PM, ....1:00 PM Where can enter any time in increments of 00:05 minutes. So will your above code hold good for the regular expression, setHours and setMinutes. Let me know please. thanks in advance. – Ruruboy Mar 16 '12 at 01:09
  • This seems to have a problem with 12:00pm. Assumming "pm" should add 12 hours is problematic. – Garr Godfrey Oct 27 '16 at 00:11