JavaScript time processing

Time processing in JavaScript

Js get current date time and other operations

var myDate = new Date();
myDate.getYear();        //Get current year (2 digits)
myDate.getFullYear();    //Get the complete year (4 digits, 1970 -????)
myDate.getMonth();       //Get the current month (0-11,0 for January)
myDate.getDate();        //Get current day (1-31)
myDate.getDay();         //Get current week X(0-6,0 for Sunday)
myDate.getTime();        //Get the current time (milliseconds from 1970.1.1)
myDate.getHours();       //Get current hours (0-23)
myDate.getMinutes();     //Get current minutes (0-59)
myDate.getSeconds();     //Get current seconds (0-59)
myDate.getMilliseconds();    //Get the current number of milliseconds (0-999)
myDate.toLocaleDateString();     //Get current date
var mytime=myDate.toLocaleTimeString();     //Get current time
myDate.toLocaleString( );        //Get date and time
  • These methods get numbers and use. toString() to convert them to strings for display.
  • To get the current month, myDate.getMonth() - 1 is required.

Define date

The Date object is used to process dates and times.
You can define the Date object with the new keyword. The following code defines a Date object named myDate:

var myDate=new Date() 

Note: the Date object automatically uses the current Date and time as its initial value.

Operation date

By using methods for date objects, we can easily manipulate dates.
In the following example, we set a specific date for the date object (August 9, 2008):

var myDate=new Date()
myDate.setFullYear(2008,7,9)

Note: the parameter indicating the month is between 0 and 11. That is, if you want to set the month to August, the parameter should be 7.

In the following example, we set the date object to a date after 5 days:

var myDate=new Date()
myDate.setDate(myDate.getDate()+5)

Note: if increasing the number of days will change the month or year, the date object will automatically complete this conversion.

Comparison date (calculated age)

let nowDate=new Date();//current time 
let myDate=new Date();
myDate.setFullYear(myYear,myMonth-1,myDay);//time of birth
let newDate=nowDate.getTime()-myDate.getTime();//Time difference in milliseconds
// Round down. For example, 10 years old and 20 days old will count as 10 years old
// If you want to round up to 11 years old, replace floor with ceil
var age = Math.ceil(newDate / 1000 / 60 / 60 / 24 /365);
if (isNaN(age)){
    age = "";
}
return age;

timer

var t;
var seconds;
/*timer*/
function timing(){
    seconds = 0;
    if (seconds > 21599) {
      let time = '6 Hour, don't want to continue gg'
      return;
    }
    t = setTimeout(function(){
        seconds=seconds+1;
        timing();
    },1000);
    formatSeconds(seconds);
}
/*Output timing text*/
function formatSeconds(seconds){
    var mins=0,hours=0,time='';
    if(seconds<3600){
      mins = parseInt(seconds /60)
      seconds=seconds%60
    }else{
      mins=parseInt(seconds/60)
      seconds=seconds%60
      hours=parseInt(mins/60)
      mins=mins%60
    }
    time=formatTime(hours)+':'+formatTime(mins)+':'+formatTime(seconds);
    return time;
}
/*The number is a bit, fill 0 in front*/
function formatTime(num){
    if(num<10)
    return '0'+num
    else
    return num+''
}
/* Stop timing */
function stopCount(){
    clearTimeout(t);
    console.log('End the game for:',seconds,'second');
}

Countdown (available now, need to be improved)

/*The number is a bit, fill 0 in front*/
function formatTime(num){
    if(num<10)
    return '0'+num
    else
    return num+''
}
var lastSeconds=$('#seconds').val();//Get to end date (in seconds here)
var timeAdd=0;
var interval=1000;
function leftTimer(){
    timeAdd=timeAdd+1;
    var leftTime=lastSeconds - timeAdd;//Time after reduction
    if(leftTime>=0){
            var days=parseInt(leftTime/60/60/24,10);//day
            var hours=parseInt(leftTime/60/60%24,10);//hour
            var minutes =parseInt(leftTime/60%60,10);//Minute
            var seconds =parseInt(leftTime%60,10);//second
            days = checkTime(days);
            hours = checkTime(hours);
            minutes = checkTime(minutes);
            seconds = checkTime(seconds);
            $('#timer').text(days+"day" + hours+"hour" + minutes+"branch"+seconds+"second");
        }else{
        //do something if the time expires
            $('#btn-refund').attr('disabled',"true").css({'pointer-events':'none','background-color':'#dddddd'});//Add disabled attribute, disable button
            return false;
        }
}
window.setInterval(function(){leftTimer();}, interval);

Tags: Javascript Attribute

Posted on Sun, 03 May 2020 03:05:46 -0400 by les4017