RED.Skull
RED.Skull

Reputation: 1736

Convert UTCString to yyyy-mm-dd

I am trying UTCString to above format. I can able to convert, problem is after conversion it shows a day before.

    var newDate = this.getCellDate(target);
    console.log(newDate); --> Dec 05 2014 00:00:00 GMT+0800 (Malay Peninsula Standard Time)
    cstDate = newDate.toISOString();
    console.log(cstDate); -- > 2014-12-04 --- > **Expected --> 2014-12-05**

Upvotes: 1

Views: 604

Answers (2)

Igor Semin
Igor Semin

Reputation: 2496

Use Date.UTC() method

var now = new Date(), // my date Thu Dec 04 2014 13:02:15 GMT+0300 (RTZ 2 (зима))
    year = now.getFullYear(),
    month = now.getMonth(),
    day = now.getDay(),
    hours = now.getHours(),
    minutes = now.getMinutes(),
    utcDate;

utcDate = new Date(Date.UTC(year, month, day, hours, minutes)); // Thu Dec 04 2014 16:02:00 GMT+0300 (RTZ 2 (зима))

Ext.Msg.alert('UTC Date', Ext.Date.format(utcDate, 'Y-m-d'));

Look at this "Thu Dec 04 2014 16:02:00" - i got utc time(+3 hours)


Fiddle example

Upvotes: 1

RED.Skull
RED.Skull

Reputation: 1736

Yeah i got the solution. I should not toISOString. instead i need to use toLocaleDateString

    custdate = newDate.toLocaleDateString();
    dueDate= custdate.split("/").reverse().join("-");

Upvotes: 0

Related Questions