timmackay
timmackay

Reputation: 1024

Get full javascript date from passing a day (number), month (name), year (number)

I'm trying to pass some date information to a date object to format the date based on where the user is hovering in a datepicker.

Using $(this) I'm able to get the day of the given month as a number. Using traversal i'm able to get the month name (February) and the year as a digit (2014).

Is it possible to take these values and apply them to a UTC date string so I can compare with my datepicker?

So far i've got:

var day = $(this).text();
var month = closest_datepicker.find('span.ui-datepicker-month').text();
var year = closest_datepicker.find('span.ui-datepicker-year').text();

which outputs '17 February 2014'

and ... well i'm not sure exactly the next step from here but I want to format this so that I either get the same format as my other dates:

Wed Feb 12 2014 00:00:00 GMT+1100 (EST)

or a millisecond value like this: 1391748657216

Upvotes: 0

Views: 1234

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You can use a library like moment.js or:

var months = ['January', 'February', 'March']
var day = '17'
var month = 'February';
var year = 2014;

var date = new Date(+year, $.inArray(month, months), +day); // $.inArray() is used instead of Array.indexOf() because of IE compatibility
console.log(date)

Upvotes: 1

Related Questions