Bhaumik Kothari
Bhaumik Kothari

Reputation: 33

Convert UTC value in dd-mm-yyyy format using javascript?

I want to convert UTC time value (e.g.1367214805) to date (in dd-mm-yyyy) format using javascript.

For e.g. in PHP if we use...

<?php Date("d-m-Y",'1367214805'); ?>

... we directly get the date in dd-mm-yyyy format.

Is there any such function in javascript? - I tried Date() in various ways in javascript but every time it is printing the present date and time!!

Thanks

Upvotes: 1

Views: 7895

Answers (5)

WesternGun
WesternGun

Reputation: 12728

Why not use getUTCxxx() methods?

function formatUTC(time_UTC_in_milliseconds_since_epoch) {
    /*
     * author: WesternGun
     */
    var time = new Date(time_UTC_in_milliseconds_since_epoch);
    var formatted = time.getUTCFullYear() + "-" 
        + (time.getUTCMonth() + 1).toString() + "-" 
        + time.getUTCDate() + " " 
        + time.getUTCHours() + ":" 
        + time.getUTCMinutes() + ":" 
        + time.getUTCSeconds();
    return formatted;
}

Remember to add 1 to the month because the range of return value of getUTCMonth() is 0~11.

With your input, we have:

formatUTC(1367214805 * 1000); // return "2013-4-29 5:53:25"

To format the numbers into \d\d form, just create another function:

function normalizeNumber(input, toAdd) {
    if (parseInt(input) < 10) {
        return toAdd + input;
    } else {
        return input;
    }
}

And use it like normalizeNumber((time.getUTCMonth() + 1).toString(), "0"), etc.

Upvotes: 1

James
James

Reputation: 82096

You could use toISOString method and just ignore everything after the T e.g.

var isoDateStr = myDate.toISOString();
isoDateStr = isoDateStr.substring(0, isoDateStr.indexOf('T'));

This would give you standard UTC date format yyyy-mm-dd. If you need to specifically format the date as dd-mm-yyyy then you can take that result and switch the values i.e.

isoDateStr = isoDateStr.split('-').reverse().join('-');

Upvotes: 1

Xotic750
Xotic750

Reputation: 23472

Take a look at Moments.js, you should be able to get whatever format you want and easily.

console.log(new Date());
console.log(moment().format("D-M-YY"));
console.log(moment(1367214805 * 1000).format("DD-MM-YY"));

jsfiddle

Upvotes: 1

Alnitak
Alnitak

Reputation: 339786

JavaScript uses millisecond epoch, so you need to multiply your number by 1000.

var t = 1367214805;
var d = new Date(t * 1000);

There are then .getUTCxxx methods to get the fields you want, and then you can just zero pad and concatenate to get the required string.

function epochToDate(t) {
    function pad2(n) {
        return n > 9 ? n : '0' + n;
    }
    var d = new Date(t * 1000);
    var year = d.getUTCFullYear();
    var month = d.getUTCMonth() + 1;  // months start at zero
    var day = d.getUTCDate();

    return pad2(day) + '-' + pad2(month) + '-' + year;
}

Upvotes: 1

neo
neo

Reputation: 801

You can try this:

myDate.toISOString().split('T')[0].split('-').reverse().join('-')

Upvotes: 0

Related Questions