How to get time form the datetime in jquery
Suppose,
datetime = Wed Mar 06 2013 11:30:00 GMT+0530 (IST)
I need only 11:30:00 from this such that
time = 11:30:00
How to get time form the datetime in jquery
Suppose,
datetime = Wed Mar 06 2013 11:30:00 GMT+0530 (IST)
I need only 11:30:00 from this such that
time = 11:30:00
check out the W3C Javascript Datetime Object Reference:
var hours = datetime.getHours(); //returns 0-23
var minutes = datetime.getMinutes(); //returns 0-59
var seconds = datetime.getSeconds(); //returns 0-59
your question:
if(minutes<10)
minutesString = 0+minutes+""; //+""casts minutes to a string.
else
minutesString = minutes;
Try this to get the current day's current time
$(document).ready(function() {
var today = new Date();
var cHour = today.getHours();
var cMin = today.getMinutes();
var cSec = today.getSeconds();
alert(cHour+ ":" + cMin+ ":" +cSec );
});
Here is a solution,
Create a Javascript Date:
var aDate = new Date(
Date.parse('Wed Mar 06 2013 11:30:00 GMT+0530 (IST)');
);
Build the output string:
var dateString = '';
var h = aDate.getHours();
var m = aDate.getMinutes();
var s = aDate.getSeconds();
if (h < 10) h = '0' + h;
if (m < 10) m = '0' + m;
if (s < 10) s = '0' + s;
dateString = h + ':' + m + ':' + s;
Documentation Links: http://www.w3schools.com/jsref/jsref_obj_date.asp
using the
Date.prototype.toLocaleTimeString
var date = new Date();
console.log(date.toLocaleTimeString('pt-BR'));
// Output: "20:30:39"
Some locales will give you different results, this is why i used the pt-BR
for the format (24h without AM/PM) (the timezone will stay the same, this will only change the format).
You can also use any other locale and pass {hour12:false}
as the options.
const event = new Date('August 19, 1975 23:15:30 GMT+00:00');
var options = {hour12:false}
console.log("en-US (options): " + event.toLocaleTimeString('en-US', options));
console.log("en-US : " + event.toLocaleTimeString('en-US'));
console.log("ar-EG (options): " + event.toLocaleTimeString('ar-EG', options));
console.log("ar-EG : " + event.toLocaleTimeString('ar-EG'));
console.log("pt-BR (options): " + event.toLocaleTimeString('pt-BR', options));
console.log("pt-BR : " + event.toLocaleTimeString('pt-BR'));
/* Output:
"en-US (options): 20:15:30"
"en-US : 8:15:30 PM"
"ar-EG (options): ٢٠:١٥:٣٠"
"ar-EG : ٨:١٥:٣٠ م"
"pt-BR (options): 20:15:30"
"pt-BR : 20:15:30"
*/