最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use toLocaleTimeString 12 hours time without AMPM abbreviations? - Stack Overflow

programmeradmin10浏览0评论

I want to show time in 12 hours format without using the AM and PM. For example 3:45 only and not 3:45 PM or 3:45 AM. How I can modify the toLocaleTimeString() to not show the PM AM but in 12 number format?

var minsToAdd = 45;
var time = "15:00";
var newTime = new Date(new Date("2000/01/01 " + time).getTime() + minsToAdd * 60000).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: true });
console.log(newTime);

I want to show time in 12 hours format without using the AM and PM. For example 3:45 only and not 3:45 PM or 3:45 AM. How I can modify the toLocaleTimeString() to not show the PM AM but in 12 number format?

var minsToAdd = 45;
var time = "15:00";
var newTime = new Date(new Date("2000/01/01 " + time).getTime() + minsToAdd * 60000).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: true });
console.log(newTime);

Share Improve this question edited Apr 2, 2019 at 21:43 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Apr 2, 2019 at 15:38 Mona CoderMona Coder 6,31618 gold badges70 silver badges137 bronze badges 5
  • Why don't you extract the required substring out of newTime? – 31piy Commented Apr 2, 2019 at 15:40
  • I was hoping to use any built in function from toLocalTimeString() – Mona Coder Commented Apr 2, 2019 at 15:41
  • It doesn't make sense, and that's why there doesn't seem any ready-made option available to do this. – 31piy Commented Apr 2, 2019 at 15:43
  • There's no preset to do that you'll just have to grab a substring that doesn't contain AM/PM – Alex W Commented Apr 2, 2019 at 15:44
  • Follow this link: enter link description here it will show you all you need. – OsamaD Commented Apr 2, 2019 at 15:45
Add a comment  | 

4 Answers 4

Reset to default 8

.toLocaleTimeString() did not have any override to do so.

There are multiple ways to do so.

Replace AM/PM by blank:

    var minsToAdd = 45;
    var time = "15:00";
    var newTime = new Date(new Date("2000/01/01 " + time).getTime() + minsToAdd * 60000).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', hour12: true });
    console.log(newTime.replace("AM","").replace("PM",""));

Using custom JavaScript function:

   function formatTime(d) {
      function z(n){return (n<10?'0':'')+n}
      var h = d.getHours();
      return (h%12 || 12) + ':' + z(d.getMinutes());
    }

    var minsToAdd = 45;
    var time = "15:00";
    var newTime = new Date(new Date("2000/01/01 " + time).getTime() + minsToAdd * 60000);
    console.log(formatTime(newTime));

it's very ez.

    const date24 = new Date();
    const data24Time = date24.toLocaleTimeString('en-IT', { hour12: false })
    console.log("24 h : ",data24Time)
    // 24 h :  20:26:09
    
    
    const date12 = new Date();
    const data12Time = date12.toLocaleTimeString('en-IT')
    console.log("12 h : ",data12Time)
    // 12 h :  8:26:09 PM
    
    
    
    
    // toLocaleTimeString('{languege for show time}-{languege for set}')

formats below assume the local time zone of the locale; America/Los_Angeles for the US

US English uses 12-hour time with AM/PM console.log(date.toLocaleTimeString('en-US')); "7:00:00 PM"

For more information, visit official docs here

Okay so the easiest way to do so is by using formatting conventions

just replace 'en-US' with 'it-IT' in your code

发布评论

评论列表(0)

  1. 暂无评论