1

I have an epoch time which I've converted to DD/MM/YYYY, and I've then converted that into a long date.

However, the format I want is "Mon Sept 06 2021", but I'm getting "Mon Sep 06 2021 00:00:00 GMT+0100 (British Summer Time)"

How can I remove everything after the year? Is it due to the Date constructor?

convertDate = (epoch) => {
  const getDate = new Date(epoch * 1).toLocaleDateString();
  console.log(getDate);

  const [dateComponents] = getDate.split(" ");
  console.log(dateComponents);

  const [day, month, year] = dateComponents.split("/");

  const date = new Date(+year, month - 1, +day);
  console.log(date);
  return date;
};
xstax
  • 96
  • 2
  • 11
  • 1
    a `Date` is always a point in time, and thus always contains a time component. If you want to display only the date portion of if you can for instance use `toLocaleDateString()` or `toDateString()`. But the default `toString()` (which you are implicitly calling with `console.log(date)`) will always display also the time – derpirscher Jun 22 '22 at 17:39
  • 1
    Yep! Used the suggested post and changed .toLocaleDateString() to .toDateString() and this sorted the issue without any other code necessary. Thank you. – xstax Jun 22 '22 at 18:02

0 Answers0