How do you display a JavaScript datetime object in the 12 hour format (AM/PM)?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
you can determine am or pm with this simple code
As far as I know, the best way to achieve that without extensions and complex coding is like this:
Javascript AM/PM Format
I found this checking this question out.
How do I use .toLocaleTimeString() without displaying seconds?
A short and sweet implementation:
Here is another way that is simple, and very effective:
Here's a way using regex:
This creates 3 matching groups:
([\d]+:[\d]{2})
- Hour:Minute(:[\d]{2})
- Seconds(.*)
- the space and period (Period is the official name for AM/PM)Then it displays the 1st and 3rd groups.
WARNING: toLocaleTimeString() may behave differently based on region / location.