This is the 17th day of my participation in Gwen Challenge

In front in the development process, sometimes need to get the current time and known time comparing demand, also has a simple access to current time and then passed on to the background, there are various kinds of need to get to the current time into a specific format to the background, it is not a complicated operation, but different products need different, sometimes need to turn the time zone. In short, there are all kinds of weird requirements, so this blog post will share the various operations related to getting the current time or date during front-end development.

JS uses the Date object to obtain the current Date and time, and uses the Date() method to obtain the current system time. The definition and usage are as follows:

1, grammar,

Date()

2, usage,

Eg: Get and print the current date and time as follows:

<script type="text/javascript"> document.write(Date()) Date.getyear (); //Date() returns the Date and time of the day. // Get the current year date.getFullYear(); Date.getmonth (); // Get the full year (4-bit,1970-xxxx) date.getmonth (); // Get the current month (0-11, 0 for January, 11 for December) date.getDate(); // Get the number of the current date object (1-31) date.getDay(); // Get the current week X(0-6, 0 for Sunday) date.getTime(); // Get the current time (milliseconds from 1970.1.1) date.gethours (); // Get the current hour (0-23) date.getminutes (); // Get the current number of minutes (0-59) date.getseconds (); // Get the current milliseconds (0-59) date.getmilliseconds (); // Get the current number of milliseconds (0-999) date.tolocaleDateString (); Var myDate= date.tolocaletimeString (); // Get the current time (eg: 10:31:52 am) var myDate2= date.totimeString (); // Get the current time (eg: 10:31:52 GMT+0800(China)) date.tolocalestring (); // Get the date and time date.tolocaleDateString (); //2021/3/26 date.toDateString(); Date.valueof () date.valueof() date.now () date.valueof() date.now ()Copy the code

The actual cases are as follows:

Step 1: Create a common method to get the year, month and day

Function getDateByType(type) {var date = new date (); var myyear = date.getFullYear(); var mymonth = date.getMonth() + 1; var myweekday = date.getDate(); if (mymonth < 10) { mymonth = '0' + mymonth; } if (myweekday < 10) { myweekday = '0' + myweekday; } if (type === "year") { return myyear; } else if (type === "month") { return myyear + '-' + mymonth; } return myyear + '-' + mymonth + '-' + myweekday; }Copy the code

Step 2: Just call the method of Step 1 where you want to get the year, month and day

1. Usage Scenario 1:
created() { this.year = this.getDateByType('year').toString(); // Get current year},Copy the code

2. Usage Scenario 2:
<div class="x-footer-l"> </div> <div class="x-footer-r"> {{getDateByType}} All rights Reserved. </div> data(){ return { getYear: this.getDateByType('year').toString(), } },Copy the code

The above is all the content of this chapter. Welcome to pay attention to the wechat public account of Sanzhan “Program Ape by Sanzhan”, and the Sina Weibo account of Sanzhan “Sanzhan 666”, welcome to pay attention!