Valijon Rahimov
Valijon Rahimov

Reputation: 41

Convert a specific string date

I have a date string like 28052019145051+05 (DDMMYYYYHHmmss) not a Unix timestamp. How can i convert it to date using date-fns? I have tried parse() but i'm getting Invalid date error.

Upvotes: 0

Views: 85

Answers (2)

Dilip Oganiya
Dilip Oganiya

Reputation: 1554

You can do this like below :

var date = new Date(unix_timestamp*1000);

var hours = date.getHours();

var minutes = "0" + date.getMinutes();

var seconds = "0" + date.getSeconds();

var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);

Upvotes: 0

Stefan Avramovic
Stefan Avramovic

Reputation: 1353

You can use substr..

var str = String(28052019145051+05)
    var DD = str.substr(2, 2);
    var MM = str.substr(0, 2);
    var YY = str.substr(4, 4);
    var HH = str.substr(8, 2);
    var Mi = str.substr(10, 2);
    var SS = str.substr(12, 2);


    console.log( YY+"-" +MM+"-"+ DD+ " "+HH+":"+Mi+":"+SS)

Output: "2019-28-05 14:50:56"

Upvotes: 1

Related Questions