JavaScript relearning, leak finding

Keywords: Javascript

JavaScript relearning, leak picking (5)

Date object

  1. Introduction to common methods of Date object
//Common methods of Date object
<script type="text/javascript">
  
  //Instantiate a Date object
  var date = new Date();
  
  //  Get current year
  var year = date.getFullYear();
  console.log("year:"+year);
  //  Note: this method of getting the month is: real month-1
  var month = date.getMonth();
  console.log("month:"+month);
  //    Get the current day of the month
  var dayM = date.getDate();
  console.log("dayM:"+dayM);
   //  What you get is the day of the week
  var dayW = date.getDay();
  console.log("dayW:"+dayW);
  //  Get the current hour
  var hour = date.getHours();
  console.log("hour:"+hour);
  //  Get the current minutes
  var minitus = date.getMinutes();
  console.log("minitus:"+minitus);
  //  Get the current seconds
  var seconds = date.getSeconds();
  console.log("seconds:"+seconds);
  
 </script>
  1. Several ways to get the current time stamp
<script type="text/javascript">
  
  //Instantiate a Date object
  var date = new Date();  
  
  //Milliseconds since 1970
  // Writing method
  var timeOne = date.getTime();
  console.log("timeOne:"+timeOne);
  // Writing method II.
  var timeTwo = date.valueOf();
  console.log("timeTwo:"+timeTwo);
  // Simple writing
  var timeThree = +new Date();
  console.log("timeThree:"+timeThree);
  // A simpler way to write
  console.log("now:"+Date.now());
  
  
   
 </script>
  1. Convert future time to timestamps
<script type="text/javascript">
  
  //Instantiate a Date object
  var date = new Date();  
  
  //  Convert Date object to string according to local time format
  var localTime = date.toTimeString();
  console.log("localTime:"+localTime);
  //  Returns the timestamp of the input event
  var inputTime = +new Date("2020-2-14 00:00:00");
  console.log("inputTime:"+inputTime);
  
  
 </script>

4. Convert Date object to string according to local time format

<script type="text/javascript">
 
 //Instantiate a Date object
 var date = new Date();  
 
 //  Convert Date object to string according to local time format
 var localTime = date.toTimeString();
 console.log("localTime:"+localTime);
 
</script>

Date object encapsulation function

1. Countdown function

function countDown(time){
  //Get the timestamp of the current time
  var nowTime = Date.now();
  //Convert input time to timestamp
  var inputTime = +new Date(time);
  //Enter timestamp - current timestamp
  var time = inputTime - nowTime;
  //Turn into heaven
  var day = parseInt(time/1000/60/60/24);
  day = day < 10 ? "0"+day : day;
  //Transformation into time
  var hour = parseInt(time/1000/60/60%24);
  hour = hour < 10 ? "0"+hour : hour;
  //Translate into fractions
  var minitues = parseInt(time/1000/60%60);
  minitues = minitues < 10 ? "0"+minitues : minitues;
  //Into seconds
  var seconds = parseInt(time/1000%60);
  seconds = seconds < 10 ? "0"+seconds : seconds;
  //Return time string
  return day + "day" + hour + "Time" +minitues + "branch" +seconds + "second";
 }

2. Time conversion function

function getNowTime(){
  
  //Instantiate time object
  var date = new Date();
  
  //  Get current year
  var year = date.getFullYear();
  year = year < 10 ? "0"+year : year;
  //  Note: this method of getting the month is: real month-1
  var month = date.getMonth() + 1;
  month = month < 10 ? "0"+month : month;
  //    Get the current day of the month
  var dayM = date.getDate();
  dayM = dayM < 10 ? "0"+dayM : dayM;
  //  Get the current hour
  var hour = date.getHours();
  hour = hour < 10 ? "0"+hour : hour;
  //  Get the current minutes
  var minitus = date.getMinutes();
  minitus = minitus < 10 ? "0"+minitus : minitus;
  //  Get the current seconds
  var seconds  = date.getSeconds();
  seconds = seconds < 10 ? "0"+seconds : seconds;
  
  return year + "year" + month + "month" + dayM + "day" + hour + "Time" +minitus + "branch" +seconds + "second";
 }

Tip:
1. A simple way to get time stamp
No parameter to get the current timestamp

+new Date() +new Date('2020-2-14 00:00:00') Date.now()
Get the current timestamp Get the time stamp of '2020-2-14 00:00:00' Get current timestamp

2. Note when getting the month:. getMonth() gets the real month - 1

43 original articles published, 43 praised, 10000 visitors+
Private letter follow

Posted by ehutchison on Tue, 11 Feb 2020 06:20:03 -0800