Converting Milliseconds to a Date (Jquery/Javascript)

Converting milliseconds to a date (jQuery/JavaScript)

var time = new Date().getTime(); // get your number
var date = new Date(time); // create Date object

console.log(date.toString()); // result: Wed Jan 12 2011 12:42:46 GMT-0800 (PST)

How to Convert Milliseconds to Date in JS?

var d = new Date(+milliseconds);

How to convert milliseconds into a readable date?

Using the library Datejs you can accomplish this quite elegantly, with its toString format specifiers: http://jsfiddle.net/TeRnM/1/.

var date = new Date(1324339200000);

date.toString("MMM dd"); // "Dec 20"

How to convert milliseconds to date and time?

Date's constructor takes a number, not a string. Either feed it in directly:

date = 1475235770601; // Note the lack of quotes making it a number

Or, if you already have a string, convert it explicitly:

date = parseInt(date);

convert from milliseconds to date format in javascript

var milliseconds = new Date().getTime(); //Get milliseconds
var date = new Date(milliseconds); // Create date from milliseconds
console.log(date.toString()); //Log: Fri Feb 05 2016 12:41:18 GMT+0200 (EET)

// Let's create futureDate, 5days after now ...
var millisecondsInDay = 8.64e+7; // Milliseconds per day
var futureDate = new Date(milliseconds + 5*millisecondsInDay);
console.log(futureDate.toString()); //Log: Wed Feb 10 2016 12:41:18 GMT+0200 (EET)

How to convert milliseconds to a readable date with Javascript?

JavaScript doesn’t have built-in date formatting. You can do it yourself, but there are also a few libraries out there.

function pad(s, width, character) {
return new Array(width - s.toString().length + 1).join(character) + s;
}

var maxDate = new Date(1407267771429);
var maxDateFormatted =
maxDate.getFullYear() +
' ' + pad(maxDate.getMonth() + 1, 2, '0') +
' ' + pad(maxDate.getDate(), 2, '0');


Related Topics



Leave a reply



Submit