Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date from 'Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)' to 'YYYY-MM-DD' in javascript

Tags:

How can I convert the time from the format "Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)" to YYYY-MM-DD.

When I try to alert() the date then this will show the date like the following - Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)

But I need the time in YYYY-MM-DD format.

Are there any built function to convert?

like image 428
Gnanendra Avatar asked Jun 09 '11 10:06

Gnanendra


People also ask

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


3 Answers

You can parse the date using the Date constructor, then spit out the individual time components:

function convert(str) {
  var date = new Date(str),
    mnth = ("0" + (date.getMonth() + 1)).slice(-2),
    day = ("0" + date.getDate()).slice(-2);
  return [date.getFullYear(), mnth, day].join("-");
}

console.log(convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)"))
//-> "2011-06-08"

As you can see from the result though, this will parse the date into the local time zone. If you want to keep the date based on the original time zone, the easiest approach is to split the string and extract the parts you need:

function convert(str) {
  var mnths = {
      Jan: "01",
      Feb: "02",
      Mar: "03",
      Apr: "04",
      May: "05",
      Jun: "06",
      Jul: "07",
      Aug: "08",
      Sep: "09",
      Oct: "10",
      Nov: "11",
      Dec: "12"
    },
    date = str.split(" ");

  return [date[3], mnths[date[1]], date[2]].join("-");
}

console.log(convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)"))
//-> "2011-06-09"
like image 168
Andy E Avatar answered Sep 24 '22 19:09

Andy E


The easiest way for me to convert a date was to stringify it then slice it.

var event = new Date("Fri Apr 05 2019 16:59:00 GMT-0700 (Pacific Daylight Time)");

let date = JSON.stringify(event)
date = date.slice(1,11)

// console.log(date) = '2019-04-05'
like image 17
rachwongrw Avatar answered Sep 22 '22 19:09

rachwongrw


function convert(str) {
    var date = new Date(str),
        mnth = ("0" + (date.getMonth()+1)).slice(-2),
        day  = ("0" + date.getDate()).slice(-2);
        hours  = ("0" + date.getHours()).slice(-2);
        minutes = ("0" + date.getMinutes()).slice(-2);
    return [ date.getFullYear(), mnth, day, hours, minutes ].join("-");
}

I used this efficiently in angular because i was losing two hours on updating a $scope.STARTevent, and $scope.ENDevent, IN console.log was fine, however saving to mYsql dropped two hours.

var whatSTART = $scope.STARTevent;
whatSTART = convert(whatever);

THIS WILL ALSO work for END

like image 4
Richard - IMPI MEDIA Avatar answered Sep 21 '22 19:09

Richard - IMPI MEDIA