The following returns time in microseconds, for example 4565212462.
alert( $.now() );
How do I convert it to a human readable time format,
For local time in ISO8601
for SQL TIMESTAMP
you could try:
var tzoffset = (new Date()).getTimezoneOffset() * 60000;
var localISOTime = (new Date(Date.now() - tzoffset))
.toISOString()
.slice(0, 19)
.replace('T', ' ');
$('#mydatediv').val(localISOTime);
Convert a Date
object to an string, using one of Date.prototype's conversion getters, for example:
var d = new Date();
d+''; // "Sun Dec 08 2013 18:55:38 GMT+0100"
d.toDateString(); // "Sun Dec 08 2013"
d.toISOString(); // "2013-12-08T17:55:38.130Z"
d.toLocaleDateString() // "8/12/2013" on my system
d.toLocaleString() // "8/12/2013 18.55.38" on my system
d.toUTCString() // "Sun, 08 Dec 2013 17:55:38 GMT"
Or, if you want it more customized, see the list of Date.prototype's getter methods.
jQuery's $.now() is an alias of new Date().getTime(), an internal Javascript function.
http://api.jquery.com/jquery.now/
This returns the number of seconds elapsed since 1970, commonly referred to (not necessarily correctly) as Unix Time, Epoch or Timestamp, depending on the circles you fall in. It can be really handy for calculating the difference between dates/times using simple maths. It doesn't have any TimeZone information and is always UTC.
http://en.wikipedia.org/wiki/Unix_time
There is no need to use jQuery as other than this alias, it does little else to help with date/time manipulation.
If you are looking for a quick and dirty way of representing the time in text, the Javascript Date object has a "toString" prototype that will return an ISO formatted Date Time.
new Date().toString();
//returns "Thu Apr 30 2015 14:37:36 GMT+0100 (BST)"
More than likely though, you will want to customize your formatting. The Date object has the ability to pull out your relevant details so you can build your own string representation.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
var d = new Date(); //without params it defaults to "now"
var t = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
//Will return 14:37:36
However, as you have asked for a jQuery solution - it is perhaps likely that you are working with older browsers. If you want to do more specific things - especially interpreting strings into Date objects (useful for API responses), you might want to look at Moment.js.
http://momentjs.com/
This will ensure cross browser compatibility and has much improved formatting without having to concatenate lots of strings to together! For example:
moment().format('hh:mm:ss');
//Will return 14:37:36
console.log(
new Date().toLocaleString().slice(9, -3)
, new Date().toString().slice(16, -15)
);
The following
function gettzdate(){
var fd = moment().format('YYYY-MM-DDTHH:MM:ss');
return fd ;
}
works for forcing the current date onto a <input type="datetime-local">
You may try like this:
new Date($.now());
Also using Javascript you can do like this:
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
document.write(time);