-1

I have string like

Tue Jun 01 2021 09:55:41 GMT+0500 (Pakistan Standard Time)

How I can convert it into date

2021-06-07
Bilal Arshad
  • 531
  • 3
  • 11
  • 33
  • Is `Tue Jun 01 2021 09:55:41 GMT+0500 (Pakistan Standard Time)` a string or a Date object? – Manas Khandelwal Jun 07 '21 at 04:58
  • this one is string – Bilal Arshad Jun 07 '21 at 04:59
  • 1
    This might help you https://stackoverflow.com/questions/23593052/format-javascript-date-as-yyyy-mm-dd – Anand G Jun 07 '21 at 05:04
  • Look at Moment.JS (https://momentjs.com/) – Will Walsh Jun 07 '21 at 05:05
  • @WillWalsh Can you elaborate a bit on why you've suggested adding a dependency on a large-size library when this could be completed easily by the built-in `Date` API available in all modern JavaScript execution environments? – esqew Jun 07 '21 at 05:09
  • @esqew - Because I thought that the OP would likely have tried the built-in `Date` API, and personally I wasn't 100% sure whether it could handle it whereas I knew that MomentJS could – Will Walsh Jun 07 '21 at 05:12
  • That timestamp is in one of the formats supported by ECMA-262, so you can just do `new Date(string)`. However, read the accepted answer at the duplicate and also [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) The format you want is given by [*toLocaleDateString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString) with the en-CA language tag, so `new Date('Tue Jun 01 2021 09:55:41 GMT+0500 (Pakistan Standard Time)').toLocaleDateString('en-CA')`. – RobG Jun 07 '21 at 11:46

4 Answers4

2

Convert it into Date object and than into string?

var date = new Date("Tue Jun 01 2021 09:55:41 GMT+0500 (Pakistan Standard Time)");
console.log(date.getFullYear() + "-" + String(date.getMonth() + 1).padStart(2, 0) + "-" + String(date.getDay()).padStart(2, 0));
vanowm
  • 9,466
  • 2
  • 21
  • 37
0

Date is represented as a standard with timestamp. You can't extract part if date and make its type as date. It would be string only. For formatting there are built in function like Intl. I am attaching here sample as well as link to Init documentation. You can explore more here Intl

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0, 200));

options = {
  year: 'numeric', month: 'numeric', day: 'numeric',
  timeZone: 'America/Los_Angeles'
};
var d= new Intl.DateTimeFormat('en-US', options).format(date);
console.log(d);
anand shukla
  • 666
  • 5
  • 14
0

Try this,

var date = new Date("Tue Jun 01 2021 09:55:41 GMT+0500 (Pakistan Standard Time)");
const formatDate = date => [
    date.getFullYear(),
    (date.getMonth() + 1).toString().padStart(2, '0'),
    date.getDate().toString().padStart(2, '0')
].join('-');
console.log(formatDate);

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

join('-') : adding '-' symbol between every elements(for concatenation)

getMonth()+1 - Since the month starts with 0, 1 is added.

RGA
  • 303
  • 3
  • 11
-1

Create Date object from string and then use toISOString() method to convert it to required format YYYY-mm-dd.

const convertDate = (dateStr) => {
  const myDate = new Date(dateStr)
  return myDate.toISOString().split("T")[0];
}

console.log(convertDate("Tue Jun 01 2021 09:55:41 GMT+0500 (Pakistan Standard Time)"));
Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22