1

It displays d11l6 changin1urren6t6161date and 1ti6

I am trying to transform what I hav16y 1516022616t 16.331`1

function timeCount() {
  var today = new Date();
  var day = today.getDate();
  var month = today.getMonth() + 1;
  var year = today.getFullYear();

  var hour = today.getHours();
  if (hour < 10) hour = "0" + hour;

  var minute = today.getMinutes();
  if (minute < 10) minute = "0" + minute;

  var second = today.getSeconds();
  if (second < 10) second = "0" + second;

  document.getElementById("clock").innerHTML = day + "/" + month + "/" + year + " | " + hour + ":" + minute + ":" + second;
  setTimeout("timeCount()", 1000);
}
<body onload="timeCount();">
  <div id="clock"></div>
</body>
foxystart
  • 13
  • 3
  • https://stackoverflow.com/questions/24998624/day-name-from-date-in-js and https://stackoverflow.com/questions/1643320/get-month-name-from-date – epascarello Feb 15 '22 at 15:48

2 Answers2

1

You can do it without any library like this:

var monthNames = ['January', 'February', 'March', 'April', 'May', 
  'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var dayOfWeekNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 
  'Thursday', 'Friday', 'Saturday'];

function timeCount() {
  var today = new Date();
  var day = today.getDate();
  var dayOfWeek = today.getDay();
  var month = today.getMonth();
  var year = today.getFullYear();

  var hour = today.getHours();
  if (hour < 10) hour = "0" + hour;

  var minute = today.getMinutes();
  if (minute < 10) minute = "0" + minute;

  var second = today.getSeconds();
  if (second < 10) second = "0" + second;

  document.getElementById("clock").innerHTML = 
    dayOfWeekNames[dayOfWeek] + ', ' +
    monthNames[month] + ' ' +
    day + ', ' + year + ', at ' +
    hour + ':' + minute;
  setTimeout("timeCount()", 1000);
}
<body onload="timeCount();">
  <div id="clock"></div>
</body>
Xeelley
  • 1,081
  • 2
  • 8
  • 18
0

Edited

try this code instead

const currentDate = new Date().toLocaleDateString("en-US", {dateStyle: "full"});
console.log(currentDate);

You can learn more about toLocaleDateString() method here https://www.w3schools.com/jsref/jsref_tolocalestring.asp

this is exactly what you wanna do

const date = new Date();
const formattedDate = date.toLocaleDateString("en-US", {dateStyle: "full"}) + " at " + date.getHours() + ":" + date.getMinutes();
console.log(formattedDate);