Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

I used the same exact syntax you did in this JSFiddle.

var n = new Date($('#date').val());
var w = n.getDay();
console.log(w);

Without us knowing what '#myFieldID' is, we can't get the question, but the syntax you used is correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top