-2

How can I see this time as HH:MM:SS using javascript?

2019-07-25T09:35:38.193896+00:00

I've tried this code below but I got only my zone's time.

var timeDate = new Date("2019-07-25T02:47:08.159794-07:00");
console.log(timeDate.toString());
var sa = timeDate.getHours();
var da = timeDate.getMinutes();
var san = timeDate.getSeconds();
console.log(sa);
console.log(da);
console.log(san);

I expect to see this time in HH:MM:SS format.

Güney
  • 91
  • 2
  • 9

6 Answers6

3
(new Date()).toUTCString().substring(17,25)

Output is

"09:58:59"
ajaykumar mp
  • 487
  • 5
  • 12
0
 var d1 = new Date();
 d1.toUTCString();
 "Thu Jul 25 2019 15:15:06 GMT+0530"
0
var timeDate = new Date("2019-07-25T09:35:38.193896+00:00");
console.log(`${timeDate.getUTCHours()}:${timeDate.getUTCMinutes()}:${timeDate.getUTCSeconds()}`);
Akash
  • 51
  • 7
0

function myFunction() {
  var d = new Date();
  var n = d.toLocaleTimeString();
  document.getElementById("demo").innerHTML = n;
}
<p id="demo"></p>
<button onclick="myFunction()">Try it</button>

Return the time portion of a Date object as a string, using locale conventions:

var d = new Date();
var n = d.toLocaleTimeString();

I found this on: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tolocaletimestring

0

There is two javascript fucntions for getting the Hours and Minutes:

Define a date like below:

var birthday = new Date('December 31, 1975, 23:15:30 GMT+11:00');

Use getUTCHours for getting hours:

console.log(birthday.getUTCHours());
// expected output: 23

Use getUTCMinutes for getting minutes:

console.log(birthday.getUTCMinutes());
// expected output: 15

See the mozilla documentation:

JavaScript Demo: Date.getUTCHours()

JavaScript Demo: Date.getUTCMinutes()

Mselmi Ali
  • 1,139
  • 2
  • 18
  • 28
  • @Guney Ral, still have issues ? do not forget to accept the solution if is helpful so other people can profit from it as well. – Mselmi Ali Jul 26 '19 at 14:42
0

The toLocaleTimeString() method returns a string with a language sensitive representation of the time portion of this date. The new locales and options arguments let applications specify the language whose formatting conventions should be used and customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation dependent. Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString

function getTime() {
    let date = new Date();
    let time = date.toLocaleTimeString();
    document.getElementById("demo").innerHTML = time;
}
<p id="demo"></p>
<button onclick="getTime()">Check time</button>

You can also check it.

console.log(time.toLocaleTimeString('en-US'));
// expected output: 1:15:30 AM

console.log(time.toLocaleTimeString('it-IT'));
// expected output: 01:15:30

console.log(time.toLocaleTimeString('ar-EG'));
// expected output: ١٢:١٥:٣٠ ص