w3resource

JavaScript: Get lowercase Ante meridiem and Post meridiem

JavaScript Datetime: Exercise-30 with Solution

Write a JavaScript function to get lowercase Ante meridiem and Post meridiem.

Sample Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript function to get lowercase Ante meridiem and Post meridiem. </title>
</head>
<body>

</body>
</html>

JavaScript Code:

function lower_am_pm(dt) 
{ 
   return dt.getHours() < 12 ? 'AM' : 'PM';
}

dt = new Date(); 
console.log(lower_am_pm(dt)); 

dt = new Date(1989, 10, 1); 
console.log(lower_am_pm(dt));

Sample Output:

pm
am

Flowchart:

Flowchart: JavaScript - Get lowercase Ante meridiem and Post meridiem

Live Demo:

See the Pen JavaScript - Get lowercase Ante meridiem and Post meridiem-date-ex-30 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript function to get a two digit representation of a year.
Next: Write a JavaScript function to get uppercase Ante meridiem and Post meridiem.

What is the difficulty level of this exercise?



JavaScript: Tips of the Day

Converts the given array elements into <li> tags and appends them to the list of the given id.

Example:

const tips_arrayToHtmlList = (arr, listID) =>(el => ((el = document.querySelector('#' + listID)),(el += arr.map(item => '<li>${item}</li>').join(''))))();
console.log(tips_arrayToHtmlList(['item 1', 'item 2'], 'myListID'));

Output:

"null
  • ${item}
  • ${item}
  • "