Get the weekday from a Date object or date string using JavaScript -


i have date string in (yyyy-mm-dd) format, how can weekday name it?

example:

  • for string "2013-07-31", output "wednesday"
  • for today's date using new date(), output based on current day of week

use function, comes date string validation:

if include function somewhere in project,

// accepts date object or date string recognized date.parse() method function getdayofweek(date) {   var dayofweek = new date(date).getday();       return isnan(dayofweek) ? null : ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'][dayofweek]; } 

you able use anywhere this:

getdayofweek("2013-07-31") > "wednesday"  getdayofweek(new date()) > // (will return today's day. see demo jsfiddle below...) 

if invalid date string used, null returned.

getdayofweek("~invalid~"); > null 

valid date strings based on date.parse() method described in mdn javascript reference.

demo: http://jsfiddle.net/samliew/fo1nnsgp/


of course can use moment.js plugin, especially if timezones involved.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -