ymd

JavaScript component to return the year, month, and day string. i.e. returns YYYY-MM-dd from Date object.

ymd

JavaScript component to return the year, month, and day string. i.e. returns YYYY-MM-dd from Date object.

It's a one liner that I got tired of writing over and over again.

Here it is:

function ymd (date) {
  return date.getFullYear() + '-' + ('0' + (1 + date.getMonth())).slice(-2) + '-' + ('0' + date.getDate()).slice(-2)
}
npm install --save ymd
component install jprichardson/ymd
  • returns using local timezone
  • returns using UTC timezone
var ymd = require('ymd')
 
ymd.utc(new Date('2013-03-05')) //2013-03-05 
 
// input to Date() is still being parsed as UTC, may be different for your timezone 
ymd(new Date('2013-03-05 4:43 PM')) //2013-03-05 

(MIT License)

Copyright 2013, JP Richardson jprichardson@gmail.com