1

I'm trying to create a function the will create a list of dates formatted in a 6 digit number starting from today till August of 2018. the result should be something like this:

[190322, 190321, 190320, ...]

I'm not sure if there is a built is a way to get the date in this 6 digit format?

tito.300
  • 976
  • 1
  • 9
  • 22
  • 1
    Pop open your developer tools(F12), go to the console, type `Date.prototype.` and look at all the options you have. Or look at the [MDN Date prototype documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/prototype) – Taplar Mar 22 '19 at 22:09
  • 1
    Best bet is to use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString then string.split('T')[0].split('-').join('') – Iskandar Reza Mar 22 '19 at 22:17
  • Did Y2K teach us nothing? Two digit years are a bad idea, *especially* in this wildly non-standard form. – tadman Mar 22 '19 at 22:41
  • 1
    I'm using this to download urls that use this format in the files name. I have no control over the naming – tito.300 Mar 22 '19 at 22:46
  • You really should do a search before asking a question, there are a huge number of duplicates for "[*how to format a date*](https://stackoverflow.com/search?q=%5Bjavascript%5D+how+to+format+a+date)". – RobG Mar 24 '19 at 10:37

2 Answers2

2

There is no build in "one function do it all" to get your outcome right away.

Option 1:

However you can use the provided functions getFullYear, getMonth and getDate to get to your result:

let d = new Date()
let formatted = d.getFullYear().toString().slice(2,4) +
(d.getMonth()+1 > 10 ? d.getMonth()+1 : `0${d.getMonth()+1}`) +
(d.getDate() > 10 ? d.getDate() : `0${d.getDate()}`)-0

Lets get through it line by line

// Uses the getFullYear function which will return 2019, ...
d.getFullYear().toString().slice(2,4) // "19"
// getMonth returns 0-11 so we have to add one month, 
// since you want the leading zero we need to also 
// check for the length before adding it to the string
(d.getMonth()+1 < 10 ? d.getMonth()+1 : `0${d.getMonth()+1}`) // "03"
// uses getDate as it returns the date number; getDay would 
// result in a the index of the weekday
(d.getDate() < 10 ? d.getDate() : `0${d.getDate()}`) // "22"
// will magically convert the string "190322" into an integer 190322
-0

Might be worth saying that this is a quick "how to achieve" it without installing any npm package but make sure to cover edge cases yourself as there are many when it comes to dates.

Option 2:

Another option would be to go for the toISOString and use split, a bit of regex, and slice to receive your outcome:

d.toISOString().split('T')[0].replace(/\-/g, '').slice(2,8)-0

Again step by step with the output:

d.toISOString() // "2019-03-22T22:13:12.975Z"
d.toISOString().split('T') // (2) ["2019-03-22", "22:13:12.975Z"]
d.toISOString().split('T')[0] // "2019-03-22"
d.toISOString().split('T')[0].replace(/\-/g, '') // "20190322"
d.toISOString().split('T')[0].replace(/\-/g, '').slice(2,8) // "190322"
d.toISOString().split('T')[0].replace(/\-/g, '').slice(2,8)-0 // 190322
DominikAngerer
  • 6,354
  • 5
  • 33
  • 60
  • 1
    Thanks! I was able to do it with the second option with a little modification: let d = new Date().toISOString(); d.split('T')[0].split('-').join('').slice(2, 8); – tito.300 Mar 22 '19 at 22:35
  • 1
    your option 1 uses `> 2` when it should be checking for `> 1` – WilliamNHarvey Mar 22 '19 at 22:38
  • 1
    Be aware that `toISOString` renders a UTC date, so if local time and UTC equivalent are on two different days you may get undesired results. – trincot Mar 22 '19 at 22:45
  • @Asthmatic good catch but a check for the length was wrong all along - needed to check for the number itself being > 10 as length would only be possible on the string :) – DominikAngerer Mar 22 '19 at 22:45
  • Checking for length that way worked when it was a string but treating is as an integer like you changed it to works just as well – WilliamNHarvey Mar 22 '19 at 22:46
2

Assisted by Date objects, you could proceed like this:

function getDateNumsBetween(a, b) {
    // b should come before a
    a = new Date(a); // new instance to avoid side effects.
    const result = [];
    while (a >= b) {
        result.push(((a.getFullYear()%100)*100 + a.getMonth()+1)*100 + a.getDate());
        a.setDate(a.getDate()-1);
    }
    return result;
}

const result = getDateNumsBetween(new Date(), new Date("August 1, 2018"));
console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286