-1

I am using Date.js to parse string of Date and Time

var start_time='10-Jun-15 12:00 am';
var end_time='10-Jun-15 12:00 pm';

when alerts

alert(Date.parse(start_time)); 
alert(Date.parse(end_time));

the first one shows

 Wed Jun 10 2015 12:00:00 GMT+0530 (India Standard Time)

and the second one shows

null

how to solve this?.thanks in advance

RobG
  • 142,382
  • 31
  • 172
  • 209
shahanas sulthan
  • 297
  • 1
  • 6
  • 17
  • 2
    Works fine for me, are you sure thats what you tried? –  Jun 03 '15 at 07:04
  • instead of Date.parse why you don't try `new Date(start_time)` – Vrushali Pawar Jun 03 '15 at 07:06
  • 1
    Aren't you supposed to use quotation marks? – Rey Libutan Jun 03 '15 at 07:06
  • yes the same problem exists. – shahanas sulthan Jun 03 '15 at 07:11
  • I am using jquery DateTime Picker to select the date and time.but this shows null only in the case of 12:00 pm – shahanas sulthan Jun 03 '15 at 07:16
  • Make a fiddle that reproduces the problem with a jquery picker. I suppose you provide Date literals from screen: how Picker displays them. Not from debug when you can read actual picker value – Kirill Slatin Jun 03 '15 at 07:21
  • 1
    Are you sure you should be using a library that hasn't been touched in 7 years and has minimal (almost zero) documentation? And yes, `Date.parse('10-Jun-15 12:00 PM')` returns *null* but `am` returns a valid Date. – RobG Jun 03 '15 at 07:31
  • Please refer [Date parsing in javascript is different between safari and chrome] [1]: http://stackoverflow.com/questions/6427204/date-parsing-in-javascript-is-different-between-safari-and-chrome – Sachin I Jun 03 '15 at 07:38
  • @RobG ,I am using that old js.How to solve this – shahanas sulthan Jun 03 '15 at 08:11
  • @SachinIngale—the OP is using Datejs, which replaces the native *Date.parse*, so differences between native implementations aren't relevant. – RobG Jun 03 '15 at 13:21

3 Answers3

0

Edited:

I've checked your date format in both chrome and FF. Firefox doesn't understand that format, but in chrome it works fine.

I used '10 Jun 2015 12:00 pm' format and it worked fine in both browsers.

So you should probably try removing dashes and use full year or use any other date format.

The date should be in RFC2822 or ISO 8601 date format.

Kremnev Sergey
  • 332
  • 1
  • 8
0

The defect you describe occurs for me too in Safari, Firefox and Chrome. Rather than fix the library, here's a simple function to parse your date format. The 2 digit years are not a good idea, the function treats them as +2000. Three and four digit years are treated as is.

// Parse date format dd-mmm-yy hh:mm ap
// Two digit years are treated as 2000
// Years of three digits or more are treated as is
function parseDmY(s) {
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
                jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
  var b = s.split(/[- :]/);
  var h = (b[3]%12) + (/pm/i.test(b[5])? 12 : 0);
  var y = +b[2] + (b[2] < 100? 2000 : 0)
  return new Date(y, months[b[1].toLowerCase()], b[0], h, b[4]);
}

console.log(parseDmY('10-Jun-2015 12:00 pm'));

Two digit years can also be treated by framing within a range of say -70 to +30 of the current year.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

If your date string is always provided in that format, using Date.parseExact will solve the problem... and it's more efficient than using Date.parse.

Example

var start_time='10-Jun-15 12:00 am';
var end_time='10-Jun-15 12:00 pm';

alert(Date.parseExact(start_time, 'dd-MMM-yy hh:mm tt'));
alert(Date.parseExact(end_time, 'dd-MMM-yy hh:mm tt'));

Hope this helps.

geoffrey.mcgill
  • 2,375
  • 1
  • 13
  • 21