user2571510 user2571510 -4 years ago 120
Javascript Question

JavaScript / jQuery: get numeric weekday value of date string

I have a field with a date value that is always formatted as YYYY-MM-DD.
How can I get the weekday value of this field value.

Example: If the date in the field is a Monday the result should be 1, if it's Tuesday it should be 2 and so on.

I tried the following but this returns NaN for me:

var n = new Date( $('#myFieldID').val() );
var w = n.getDay();


I am using Moment.js to create the date that goes into that field:

var dateNow = moment().format('YYYY-MM-DD');
$('#myFieldID').val(dateNow);


This works and would fill in 2014-02-13 for the 13th of February 2014.

Many thanks for any help with this, Tim

Answer Source

I found out that Moment.js also has a weekday function built-in which resolved the issue. Thanks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download