2

I have the following code to Display Date and time

**<div id='time'></div>
<script type="text/javascript">
 
// get a new date (locale machine date time)
var date = new Date();
// get the date as a string
var n = date.toDateString();
// get the time as a string
var time = date.toLocaleTimeString();
// find the html element with the id of time
// set the innerHTML of that element to the date a space the time
document.getElementById('time').innerHTML = n + ' ' + time;
</script>**

which display it as Wed Feb 09 2022 5:20:44 PM

what am looking for is how to get it display as :

Wednesday 9 February, 05:20 PM

appreciate d your assistance

Thank you

Stéphane Millien
  • 3,238
  • 22
  • 36

1 Answers1

0

Here is a simple solution.

Note the comma is after the weekday name as this is the correct way of writing it. But it can be easily changed to be after the month name.

<div id='time'></div>
<script type="text/javascript">
 
// get a new date (locale machine datetime)

let date = new Date ();
let out = date.toLocaleString("en-GB",{weekday:'long',day:'numeric',month:'long'}) +" " +
          new Intl.DateTimeFormat("en-GB",{hour12:true,hour:'numeric',minute: 'numeric'}).format(date);


document.getElementById('time').innerHTML = out;
</script>
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42