2

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
Developer
  • 1,030
  • 6
  • 14
  • 37

5 Answers5

9

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;
最白目
  • 3,505
  • 6
  • 59
  • 114
  • Thanks but how can i make it to show like hh : mm : ss format. Now when its 12 then it shows 12:0:0, i want 12:00:00. – Developer Mar 06 '13 at 11:33
5

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 );
});
Swarne27
  • 5,521
  • 7
  • 26
  • 41
5

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

  • var datetime is not a string. Date.parse expects a string. – 最白目 Mar 06 '13 at 11:34
  • Stuart Eske: Thanks but how can i make it to show like hh : mm : ss format. Now when its 12 then it shows 12:0:0, i want 12:00:00. – Developer Mar 06 '13 at 11:34
  • There is no easy way to do this, take a look at this link. [Link](http://stackoverflow.com/questions/6979697/javascript-date-leading-0-for-days-and-months-where-applicable) @Developer – Stuart Eske Mar 06 '13 at 11:39
1

What about using the excellent momentjs

adrian
  • 111
  • 3
  • 11
1

You can do it very easily with JavaScript.

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"
*/
LMeirelles
  • 13
  • 6