let unix_timestamp = 1549312452;
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(unix_timestamp * 1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(formattedTime);
For more information regarding the Date object, please refer to MDN or the ECMAScript 5 specification.
Answer from Aron Rotteveel on Stack Overflowlet unix_timestamp = 1549312452;
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(unix_timestamp * 1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(formattedTime);
For more information regarding the Date object, please refer to MDN or the ECMAScript 5 specification.
function timeConverter(UNIX_timestamp){
var a = new Date(UNIX_timestamp * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
return time;
}
console.log(timeConverter(0));
How can I convert "2020-11-18-05:12" into unix timestamp? and then add 5 hours to it?
javascript - How to convert date in format "YYYY-MM-DD hh:mm:ss" to UNIX timestamp - Stack Overflow
javascript - how convert unixtime to yy-mm-dd - Stack Overflow
node.js - Function to convert timestamp to human date in javascript - Stack Overflow
so if I have a string that is in the format of 'yyyy-mm-dd-HH:MM' , then how can I convert that into a unix timestamp?
if I get the unix timestamp, then I could just add however many seconds there are in 5 hours and add it (assuming the unix timestamp is in seconds)
Use the long date constructor and specify all date/time components:
var match = '2011-07-15 13:18:52'.match(/^(\d+)-(\d+)-(\d+) (\d+)\:(\d+)\:(\d+)$/)
var date = new Date(match[1], match[2] - 1, match[3], match[4], match[5], match[6])
// ------------------------------------^^^
// month must be between 0 and 11, not 1 and 12
console.log(date);
console.log(date.getTime() / 1000);
Following code will work for format YYYY-MM-DD hh:mm:ss:
function parse(dateAsString) {
return new Date(dateAsString.replace(/-/g, '/'))
}
This code converts YYYY-MM-DD hh:mm:ss to YYYY/MM/DD hh:mm:ss that is easily parsed by Date constructor.
You could use a library like moments.js or in POJS
Moments.js
A 5.5kb javascript date library for parsing, validating, manipulating, and formatting dates.
unixtime
Unix time, or POSIX time, is a system for describing instances in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,[note 1] not counting leap seconds.[note 2] It is used widely in Unix-like and many other operating systems and file formats. Due to its handling of leap seconds, it is neither a linear representation of time nor a true representation of UTC.[note 3] Unix time may be checked on some Unix systems by typing date +%s on the command line.
Javascript Date object
Summary
Creates JavaScript Date instances which let you work with dates and times.
Javascript
function padZero(number) {
if (number < 10) {
number = "0" + number;
}
return number;
}
function unixtime2YYMMDD(unixtime) {
var milliseconds = unixtime * 1000,
dateObject = new Date(milliseconds),
temp = [];
temp.push(dateObject.getUTCFullYear().toString().slice(2));
temp.push(padZero(dateObject.getUTCMonth() + 1));
temp.push(padZero(dateObject.getUTCDate()));
return temp.join("-");
}
console.log(unixtime2YYMMDD(1372069271));
Output
13-06-24
On jsfiddle
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} var today = yyyy.toString().substr(2,2)+'-'+mm+'-'+dd;
document.getElementById("output").innerHTML = today;
This will do the job ..
Of course your new Date() will be your unix time..
http://jsfiddle.net/UpMU5/
The value 1382086394000 is probably a time value, which is the number of milliseconds since 1970-01-01T00:00:00Z. You can use it to create an ECMAScript Date object using the Date constructor:
var d = new Date(1382086394000);
How you convert that into something readable is up to you. Simply sending it to output should call the internal (and entirely implementation dependent) toString method* that usually prints the equivalent system time in a human readable form, e.g.
Fri Oct 18 2013 18:53:14 GMT+1000 (EST)
In ES5 there are some other built-in formatting options:
- toDateString
- toTimeString
- toLocaleString
and so on. Note that most are implementation dependent and will be different in different browsers. If you want the same format across all browsers, you'll need to format the date yourself, e.g.:
alert(d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear());
* The format of Date.prototype.toString has been standardised in ECMAScript 2018. It might be a while before it's ubiquitous across all implementations, but at least the more common browsers support it now.
This works fine. Checked in chrome browser:
var theDate = new Date(timeStamp_value * 1000);
dateString = theDate.toGMTString();
alert(dateString );
I resolved the issue with the following:
var date = new Date(indexPie);
var year = date.getUTCFullYear();
var month = date.getUTCMonth() + 1;
var day = date.getUTCDate();
var dateString = year + "-" + month + "-" + day;
You are expecting that the value in date variable: "2014-05-01" will be parsed as in local timezone, but actually it is parsed as in UTC.
You can convert the date from UTC to local timezone like this:
var newDate = new Date(indexPie + new Date().getTimezoneOffset() * 60000);