In this approach, we will change the DateTime format by only using native methods. Simply put, we will apply modulo “%” operator to find the hour in 12-hour format and use conditional “?:” operator to apply “AM” or “PM”.
In this approach, we will utilize an inbuilt method toLocaleString() to change the format of given date into AM-PM format.
toLocaleString(): It returns a string representation of the date Object. The 2 arguments Locale and options allow for customization of the behavior of the method.
- PHP date() format when inserting into datetime in MySQL
- What is the difference between display: inline and display: inline-block in CSS?
- p5.js | hour() function
- How do I get PHP errors to display?
- CSS | Display property
- Display div element on hovering over <a> tag using CSS
- How Facebook Display Ads for something you searched
- HTML | DOM ins dateTime Property
- HTML | DOM del dateTime Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.