Skip to content
Fix Code Error

Get String in YYYYMMDD format from JS date object?

March 13, 2021 by Code Error
Posted By: Anonymous

I’m trying to use JS to turn a date object into a string in YYYYMMDD format. Is there an easier way than concatenating Date.getYear(), Date.getMonth(), and Date.getDay()?

Solution

Altered piece of code I often use:

Date.prototype.yyyymmdd = function() {
  var mm = this.getMonth() + 1; // getMonth() is zero-based
  var dd = this.getDate();

  return [this.getFullYear(),
          (mm>9 ? '' : '0') + mm,
          (dd>9 ? '' : '0') + dd
         ].join('');
};

var date = new Date();
date.yyyymmdd();
Answered By: Anonymous

Related Articles

  • SQL JOIN and different types of JOINs
  • SQL query return data from multiple tables
  • How do I keep only the first map and when the game…
  • Current time formatting with Javascript
  • How does JavaScript .prototype work?
  • How to "properly" create a custom object in JavaScript?
  • What is the 'new' keyword in JavaScript?
  • Getting current date and time in JavaScript
  • How to convert a full date to a short date in javascript?
  • How to get default value from component vue.js?
  • How could I return a complex calculated array to my…
  • Disabling a button upon condition in Google App script
  • JQuery Date picker Range
  • Javascript add leading zeroes to date
  • INNER JOIN vs LEFT JOIN performance in SQL Server
  • How do I include certain conditions in SQL Count
  • TypeScript metadata reflection references other…
  • Coffeescript class extend more bloat than Backbone extend
  • For-each over an array in JavaScript
  • Difference in Months between two dates in JavaScript
  • How to return only the Date from a SQL Server…
  • How to format a Date in MM/dd/yyyy HH:mm:ss format…
  • Timestamp to human readable format
  • What is your most productive shortcut with Vim?
  • How to get the day of week and the month of the year?
  • java.util.Date and getYear()
  • Get week of year in JavaScript like in PHP
  • Trigger a specific build in jenkins pipeline
  • How to determine one year from now in Javascript
  • Binding an ember model's date attribute to…
  • Class vs. static method in JavaScript
  • How to find out what is referencing a detached DOM…
  • Get the name of an object's type
  • explain backbone object and class creation pattern
  • How to compare two dates to find time difference in…
  • Does the join order matter in SQL?
  • ValueError Cannot assign "
  • How to add/subtract dates with JavaScript?
  • How do I get just the date when using MSSQL GetDate()?
  • SQLGrammarException:error executing work ORA-01722:…
  • Pandas Merging 101
  • vue.js referencing constants in data?
  • Javascript code for showing yesterday's date and todays date
  • JavaScript how to get tomorrows date in format dd-mm-yy
  • I want to solve the javascript OOP problems, but my…
  • Ember build failing
  • TypeError: date.getDate is not a function ReactJS
  • Use of String.Format in JavaScript?
  • JavaScript OOP in NodeJS: how?
  • Pass "this" into nested data function Vuejs
  • Subtract days, months, years from a date in JavaScript
  • What are the nuances of scope prototypal /…
  • Why does C++ code for testing the Collatz conjecture…
  • Scroll to bottom of a modal window in Vue
  • Cannot write if or select above begin on a procedure
  • SQL Server Trigger on Specified Columns
  • How to get the first and last date of the current year?
  • What is the use of join() in Python threading?
  • Create User Profile Piece
  • How can I change format datetime to date in vue component?
  • Any reason not to use '+' to concatenate two strings?
  • Rewrite left outer join involving multiple tables…
  • Left Outer Join using + sign in Oracle 11g
  • In this tic tac toe game, why the last move of last…
  • SQL left join vs multiple tables on FROM line?
  • model returns null on controller
  • Union of multiple Database queries with same parameters
  • Where can I find documentation on formatting a date…
  • get an array of dates from today for datepicker with…
  • Database development mistakes made by application developers
  • Cleaning data with javascript and formating date
  • Using IS NULL or IS NOT NULL on join conditions -…
  • data.table vs dplyr: can one do something well the…
  • Enable and show only specific dates and today date…
  • Formatting dates in Underscore templates
  • Date format in underscore template(javascript)
  • Vue.js datepicker not allowing the update of default…
  • Javascript to display the current date and time
  • How to update TextInput with DateTimePicker value?…
  • Conditional WHERE clauses with TVP parameter
  • Get week day name from a given month, day and year…
  • How does the "this" keyword work?
  • Rename mulitple special characters in filenames
  • How do I create a custom Error in JavaScript?
  • Quickest way to do a [adsisearcher] and store in a…
  • Format date to MM/dd/yyyy in JavaScript
  • Lazy Method for Reading Big File in Python?
  • How do I switch two players in tic-tac-toe game in Python?
  • How to calculate age in T-SQL with years, months, and days
  • Measure the time it takes to execute a t-sql query
  • Get first and last date of current month with…
  • How do I correctly clone a JavaScript object?
  • Vuejs how to compare dates with filter in list
  • Javascript format date / time
  • How to change day-format (M > Mo, F > Fr...)…
  • Multiple dates with javascript
  • LINQ left join generates wrong SQL query
  • What is the difference between "INNER JOIN" and…
  • Convert an ISO date to the date format yyyy-mm-dd in…
  • SQL "select where not in subquery" returns no results

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I update pip itself from inside my virtual environment?

Next Post:

How can I add an empty directory to a Git repository?

Leave a Reply Cancel reply

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

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error