0

I am new to coding. I know HTML, CSS, and js. My question is I want to display time in my HTML page based on Operating system time format. For example, my system time format is 12 hours format I need to show 12 hours format if my system time format is 24 hours I need to show 24 hours format in my HTML page. I test myself switch time format in OS time settings. That time also page time has to change.

Is it possible to do with HTML, CSS and js?

If not is there any alternative ways to do it. Help or suggest me

Ashu
  • 2,066
  • 3
  • 19
  • 33
  • 1
    `toLocaleTimeString()`? – Niet the Dark Absol Jun 12 '19 at 15:11
  • for future reference - this site is your friend - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – Ctznkane525 Jun 12 '19 at 15:14
  • @NiettheDarkAbsol `toLocaleTimeString()` does not format the time based on your OS. My OS time is 24-hour but the output from `toLocaleTimeString()` is still 12-hour. I think that's likely the path OP will have to go down, just noting. – Tyler Roper Jun 12 '19 at 15:15

3 Answers3

0

it may help

var currentTime = new Date(),
      hours = currentTime.getHours(),
      minutes = currentTime.getMinutes();

 if (minutes < 10) {
  minutes = "0" + minutes;
  }

 var suffix = "AM";
 if (hours >= 12) {
    suffix = "PM";
    hours = hours - 12;
 }
 if (hours == 0) {
  hours = 12;
 }

 document.write(hours + ":" + minutes + " " + suffix)
Hamza Riz
  • 9
  • 2
0

not sure if we've a function to differentiate the time format and give us output based on OS time. But just in case if you've a chance to manipulate at your end, try this using JavaScript built-in functions :

For 12-hr Format :

let formattedTime = new Date().toLocaleTimeString('en-US');
console.log(formattedTime)

For 24-hr Format :

let currentDateTime = new Date();
    let formattedTime = currentDateTime.getHours() + ":" + currentDateTime.getMinutes() +":" + currentDateTime.getSeconds();
    console.log(formattedTime)

(Or)

For 24-hr Format in one line as @Edson stated :

let currentDateTime = new Date();
console.log(currentDateTime.toLocaleTimeString('en-US', { hour12: false }))
whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
0

Short answer is No, but, You can check how the system query possibilities used:

console.log(navigator)

Check this answer

Get system infos with JS Answer & Navigator documentation