0

Hey guys I am retrieving a value from an input box and am using that value to turn into a Date for JavaScript the format is Y-m-d h:i:s. It works perfect in Chrome but any other browser says invalid Date

var old = $(".checked-in-time").val();
old = new Date(old);

UPDATE:

Here is what I am doing:

var current = new Date();
var old = $(".checked-in-time").val();
old = Date.parse(old , 'Y-m-d H:i:s');
var newEnd = current - old
minutes = parseInt((newEnd/(1000*60))%60);
var subtractedWaitTime = minutes;

Pretty much getting the time difference based on minutes.

4
  • Y-m-dTH:i:s is it a valid format? Commented Jan 20, 2015 at 5:18
  • ops sorry check update Commented Jan 20, 2015 at 5:20
  • Date.parse("dateString") takes only one string parameter and returns time in milliseconds. Commented Jan 20, 2015 at 5:23
  • Yes I know which is why I was using new Date(old); but using moment was the solution i took. It works great now. Commented Jan 20, 2015 at 5:28

4 Answers 4

1

Date.parse accepts a limited number of formats:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

Your format is not one of the ones supported. You can parse it yourself and just pass the arguments directly. One of the forms Date accepts is this, which would be easy enough to pull out from your format:

new Date(year, month[, date[, hour[, minutes[, seconds[, milliseconds]]]]]);

But I would recommend removing the pain of having to worry about cross browser compatibility and parsing things yourself, and use moment instead, where you can parse the date like this

moment(dateStr,'YYYY-M-D H:m:s')

and then if you needed to have it as a Javascript Date object you could just run

moment().toDate();

in the more likely case you just need to display it formatted somewhere, or compare it to other dates, moment offers many functions for formatting, manipulating and comparing dates

http://momentjs.com/docs/#/displaying/as-javascript-date/

Sign up to request clarification or add additional context in comments.

Comments

1

Try one of the following formats

  • MM-dd-yyyy
  • yyyy/MM/dd
  • MM/dd/yyyy
  • MMMM dd, yyyy
  • MMM dd, yyyy

Could be that some browsers don't support yyyy-mm-dd

1 Comment

@David Biga it would be .parse() not .parseDate() like VPK suggested
1

You can parse date like this,

var old = $(".checked-in-time").val();
old = Date.parseDate(old , 'yyyy-mm-dd h:i:s');

if the above not working, you can also try Y-m-d H:i:s this format. For me Y/m/d H:i worked fine.

8 Comments

Keeps telling me invalid date. Here is what my input box looks like 2015-01-19 22:29:41
also Date.parseDate is not defined as a function for me?
Which jQuery version you are using?
<script src="js/jquery.js"></script> <script src="js/jquery-migrate-1.2.1.min.js"></script>
if I just do Date.parse it keeps telling me NaN
|
1

You could try use such format Y-m-dTH:i:s, e.g. 2011-01-01T12:00:00

Or you can use moment library (Javascript Date library)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.