0

I am trying to convert date(integer) to the string. For example from 2019-01-01, my output should be 01 Jan. I wrote the code below.

const dates = ['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08', '2019-01-09'];

let months = [];
let day = [];
let t_stamp = [];
for (let i = 0; i < dates.length; i++) {
  let obj = new Date(dates[i]);
  months.push(obj.toLocaleString("en-us", {
    month: "short"
  }));
  let con = dates[i].split('-');
  day.push(con[2]);
  console.log(con);
}
for (let i = 0; i < day.length; i++) {
  t_stamp[i] = day[i] + '-' + months[i];
}

This code works almost fine the only issue is, for the first date (2019-01-01) it is giving me output as 01-Dec instead of 01-Jan and for rest of the dates I am getting perfect output ('2019-01-02' = 02-Jan, '2019-01-03' = 03-Jan and so on).

Thank you in advance

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
Alpesh Ahir
  • 27
  • 1
  • 1
  • 9
  • You don't need to parse the strings to dates at all. Just parse the strings and map the month number to your required month name and format the result. Anything else is just overkill. – RobG Jun 11 '19 at 06:18

2 Answers2

1

You can try this optimized version:

const dates = ['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08', '2019-01-09'];

  let months = [];
  let day = [];
  let t_stamp = [];
  dateArray = [];
  dates.forEach(function(elem) {
    let localDate = new Date(elem).toDateString().split(' ');
    dateArray.push(localDate[2]+' '+localDate[1]);
  });
console.log(dateArray);

How This works: let's take '2019-01-01'

  1. new Date('2019-01-01') -> Tue Jan 01 2019 05:30:00 GMT+0530 (India Standard Time)
  2. new Date('2019-01-01').toDateString() -> "Tue Jan 01 2019"
  3. new Date('2019-01-01').toDateString().split(' ') ->  ["Tue", "Jan", "01", "2019"]

Once you get the list, you just need to shuffle it and utilize it.

I hope it helps.

1

The Issue

Your dates don't have times, so when you convert them to a Date object they are instantiated with a time of 00:00:000 UTC. Because toLocaleString() then converts the time to the user's local time, it's very possible that this will result in the day shifting.

For example, my timezone is UTC -4, so your date of 2019-01-01 00:00:000 UTC becomes 2018-12-31 19:00:000 EST for me.

Solution

You don't need to convert to Date at all. If you split the original string, you've already got day in the format you need. The only work is to convert the month to its shortened name. A simple lookup array will do the trick.

const dates = ['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08', '2019-01-09'];
const months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

const result = dates.map(d => {
  let [year,month,day] = d.split('-');
  return `${day}-${months[month-1]}`;
});

console.log(result);
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
  • This one works. Thanks – Alpesh Ahir Jun 11 '19 at 04:44
  • Just think about what's happening. This takes a string, parses it to a Date, uses a mostly implementation–dependent method to generate another string, parses that, then creates formatted output. Why not just split the original string, map the month number to a name, then format it. Avoids the who Date/toLocaleString cruft. – RobG Jun 11 '19 at 06:21
  • @RobG Good point. Fixed. – Tyler Roper Jun 11 '19 at 13:26