Home > Software design >  Dynamic date, month, year
Dynamic date, month, year

Time:02-16

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 onl oad="timeCount();">
  <div id="clock"></div>
</body>

CodePudding user response:

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 onl oad="timeCount();">
  <div id="clock"></div>
</body>https://stackoverflow.com/questions/71129190/dynamic-date-month-year#

CodePudding user response:

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);

  • Related