• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Computing / JavaScript get Date in YYYYMMDD Format

JavaScript get Date in YYYYMMDD Format

Last updated on June 15, 2020 by Sal Ferrarello

Recently, I was working in JavaScript and I need to transform a date into the format YYYYMMDD, i.e.

  • four digit year
  • two digit month
  • two digit day

For GMT

var date = new Date();
date.toISOString().split('T')[0].replace('-','').replace('-','');

For Local Time

var date = new Date();
return date.getFullYear().toString() +
    (1 === (date.getMonth()+1).toString().length ? '0' : '') +
    (date.getMonth()+1).toString() +
    (1 === date.getDate().toString().length ? '0' : '') +
    date.getDate().toString();

For Additional Time Formatting Options

If I need further formatting, I would use a third-party library like Luxon.

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Computing, Dev Tips, Draft, Programming, Solution Tagged With: DateTime, JavaScript

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in