Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript clock and date

window.onload = setInterval(function() {
	let today = new Date();
    let year = today.getFullYear();
    let month = today.getMonth() + 1;
	let day = today.getDate();
	let hour = today.getHours();
	let min = today.getMinutes();
	let sec = today.getSeconds();
	am_pm = "AM";

	if (hour > 12) {
		hour -= 12;
		am_pm = "PM";
	}
	if (hour == 0) {
		hour = 12;
		am_pm = "AM";
	}

	hour = hour < 10 ? "0" + hour : hour;
	min = min < 10 ? "0" + min : min;
	sec = sec < 10 ? "0" + sec : sec;
	month = month < 10 ? "0" + month : month;
	day = day < 10 ? "0" + day : day;

	let currentDate = [year, month, day].join("-")
	let currentTime = hour + ":"
			+ min + ":" + sec + " " + am_pm;

	document.getElementById("app")
			.innerHTML = currentDate + " " + currentTime;
}, 1000)
Comment

how to make a clock in js

function currentTime() {
  let date = new Date(); 
  let hh = date.getHours();
  let mm = date.getMinutes();
  let ss = date.getSeconds();
  let session = "AM";

  if(hh == 0){
      hh = 12;
  }
  if(hh > 12){
      hh = hh - 12;
      session = "PM";
   }

   hh = (hh < 10) ? "0" + hh : hh;
   mm = (mm < 10) ? "0" + mm : mm;
   ss = (ss < 10) ? "0" + ss : ss;
    
   let time = hh + ":" + mm + ":" + ss + " " + session;

  document.getElementById("clock").innerText = time; 
  let t = setTimeout(function(){ currentTime() }, 1000);
}
currentTime();
Comment

javascript clock

//you can use Timer to do yours clock
	Timer timer=new Timer();
	TimerTask task=new TimerTask(){
	int h=0;
	int s=0;
	public void run(){
		s++;
		if(s==60){
			h++;
			s=0;
			if(h==25){
				h=0;
			}
		}
	}
	};
	 timer.schedule(task,0, 1000);
 timer.schedule(task, 1000);
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: async constructor javascript 
Javascript :: displaying the date react 
Javascript :: set css var with javascript 
Javascript :: jquery header basic auth 
Javascript :: get the data from selected item in select 
Javascript :: javascript form post json data 
Javascript :: how to use foreach in javascript 
Javascript :: how to pass the data from one page to another in javascript 
Javascript :: set navigation drawer to open by default react native 
Javascript :: electron send message from renderer to main 
Javascript :: chartjs begin at 0 
Javascript :: javascript hours minutes seconds 
Javascript :: jest mock createobjecturl 
Javascript :: .textcontent in javascript 
Javascript :: add li to ul javascript 
Javascript :: JavaScript String startsWith() examples 
Javascript :: react tostify 
Javascript :: hmac_sha256 node 
Javascript :: how would you check if a number is an integer in javascript 
Javascript :: node javascript foreach limit 
Javascript :: mdn rest 
Javascript :: vue js computed 
Javascript :: javascript get same elments from multiple arrays 
Javascript :: fromcharcode in javascript 
Javascript :: capitalize a string javascript 
Javascript :: appending an element to another element javascript 
Javascript :: preview upload image js 
Javascript :: javascript key event 
Javascript :: on load page javascript 
Javascript :: quasar change port 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =