Skip to content Skip to sidebar Skip to footer

How To Convert Regular Date To Unixtime In Javascript?

How can i convert date which looks like this 12092008 to unixtime like this 1221215809

Solution 1:

You may want to use the following:

functioncovertToUnixTime(yourDate) {
    returnnewDate(yourDate.substring(4, 8) + '/' + 
                    yourDate.substring(2, 4) + '/' + 
                    yourDate.substring(0, 2)).getTime() / 1000;
}

covertToUnixTime('12092008');   // Returns: 1221170400

Solution 2:

You could use jQuery datepicker's utility functions, parseDate and formatDate.

Solution 3:

This worked for me.

var day = $("#day").val();
var month   = $("#month").val();
var year    = $("#year").val();
var date    = newDate();
date.setFullYear(year, month, day)
var unixtimeMS = date.getTime();
var unixtime = parseInt(unixtimeMS / 1000);
alert(unixtime);

Post a Comment for "How To Convert Regular Date To Unixtime In Javascript?"