The following content is used by myself in the project development, if there is a need for reference, but also welcome to leave a message for supplement and modification! Progress together 🤣Copy the code
1, obtain the current time:
// Get the current system time
var myDate = new Date(a);// Example: Wed Jan 27 2021 10:42:51 GMT+0800
// Use the timestamp and the specified date to get the Chinese standard time, and then switch the specified format
// Timestamp (milliseconds)
var myDate = new Date(1611715656370); // Example: Wed Jan 27 2021 10:47:36 GMT+0800
// Specify a date
var myDate = new Date("The 2012-12-10 12:15:15") // example: Mon Dec 10 2012 12:15:15 GMT+0800
Copy the code
Code note: After obtaining the Chinese standard time (myDate), perform the following format conversion
2, obtain the specified format time:
// Get the specified format time
// Get the current date
myDate.toLocaleDateString(); / / example: "2021/1/27"
// Get the current time
myDate.toLocaleTimeString(); // example: "9:58:30 am"
// Get the date and time
myDate.toLocaleString( ); // example: "2021/1/27 9:58:30 am"
/ / for years
myDate.getYear(); / / example: 121
myDate.getFullYear(); / / example: 2021
// Get the month ((0-11,0 = 1))
myDate.getMonth(); // Example: When the result is 2, the actual month is 2+1
// Fetch date (1-31)
myDate.getDate(); / / example: 27
// Get the day of the week (0-6,0 for Sunday)
myDate.getDay(); / / cases: 3
// Get the current hour (0-23)
myDate.getHours(); / / example: 10
// Get the current minute (0-59)
myDate.getMinutes(); / / example: 47
// Get the current number of seconds (0-59)
myDate.getSeconds(); / / example: 36
// Get the current number of milliseconds (0-999)
myDate.getMilliseconds(); / / example: 370
Copy the code
3, obtain the timestamp:
// Get the current system time
var myDate = new Date(a);// Example: Wed Jan 27 2021 10:42:51 GMT+0800
// Method one (millisecond timestamp) :
var timestamp=myDate.getTime(); / / example: 1280977330748
// Method 2 (millisecond timestamp) :
var timestamp =(myDate).valueOf(); / / example: 1280977330748
// Get the timestamp in seconds.
var timestamp = myDate.getTime() / 1000; / / example: 1280977330000
var timestamp =Date.parse(myDate) / 1000; / / example: 1280977330000
// Note: the result here converts the last three bits (ms) to 000, which may cause problems.
// This parameter is not recommended for dynamically adding page element ids.
Copy the code
4. Time conversion:
// Encapsulate method
// Get the time difference
// Start and end are times in any format
getReduceTimes(start,end){
// Get the start time and end time, calculate the difference of the timestamp (get the time difference between them)
let startDate = new Date(start).getTime();
let endDate = new Date(end).getTime();
let endReduceStart = endDate-startDate
/ / the total number of seconds
let totalSeconds = Math.floor(endReduceStart / 1000);
/ / number of days
let day = Math.floor(second / 3600 / 24);
/ / hour
let hour = Math.floor(second / 3600 % 24);
/ / minute
let minute = Math.floor(second / 60 % 60);
/ / SEC.
let second = Math.floor(second % 60);
let dateTimes = {
totalSeconds,
day,
hour,
minute,
second
}
return dateTimes; // Return the time difference object and get the desired data from it
}
Copy the code
5. Set time filter in VUE:
/ / sample:
<template>
<div>{{ a | data}}</div>
</template>
export default {
data() {
return {
a: Date.now()
}
},
filters: {data(date) {
let myDate = new Date(date);
let year = myDate.getFullYear();
let month = (myDate.getMonth() + 1) < 10 ? '0' + (myDate.getMonth() + 1) : (myDate.getMonth() + 1);
let day = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
let hour = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours();
let minutes = myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes();
let seconds = myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds();
return year+ The '-' + month + The '-' + day + ' ' + hour + ':' + minutes + ':' + seconds; // Returns the time format}}}Copy the code
5, China Standard time to Greenwich Mean Time method:
// Chinese standard time is converted to Greenwich Mean time
function formatDate(dateTime){
let timestamp = dateTime.getTime();
let newDate = new Date(dateTime + 8 * 3600 * 1000);
return newDate.toISOString();
}
let dealDate = 'Thu Apr 29 2021 15:13:21 GMT+0800 ';
const geLinDate = formatDate(new Date(dealDate));
console.log(geLinDate); / / "the T15 2021-04-29:12. 000 z"
Copy the code
Get the number of days between two time ranges:
// 1. By time:
getDay(startTime, endTime){
let startTimeStamp = Date.parse(new Date(startTime));
let endTimeStamp = Date.parse(new Date(endTime));
let date = endTimeStamp - startTimeStamp;
let days = Math.ceil(date/(3600*24*1000));
return days
}
getDay('2021-09-01'.'2021-09-14');
// 2.
getDay(startTime, endTime){
let befo reDate = startTime.getTime();
let nowDate = endTime.getTime();
let date = nowDate - beforeDate
let days = Math.ceil(date/(3600*24*1000))
return days
}
getDay(new Date("2021-09-01"),new Date()) round up:Math.floor(4.2) // 4Round down:Math.ceil(4.2) // 5
Copy the code