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.
- Java Program to Display Current Hour and Current Minute
- PHP date() format when inserting into datetime in MySQL
- PHP | DateTime format() Function
- Top 5 Skills You Must Know Before You Learn ReactJS
- 13 Things You Should Know Before You Enter In Web Development
- How Facebook Display Ads for something you searched
- What is the difference between display: inline and display: inline-block in CSS?
- How to display a list in n columns format ?
- Java Program Format Time in MMMM Format
- p5.js | hour() function
- Get the Hour from timestamp in Pandas
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.