In web development, we often need to change the date format, such as converting dates in yyyy-MM-dd format to dd/MM/yyyy format, or converting dates into timestamps. In this article, we will introduce how to change the date format using JavaScript.
1. Convert date to timestamp
Timestamp refers to the number of milliseconds since January 1, 1970 00:00:00 UTC. It is often used to record time and date distribution . In JavaScript, we can use the getTime() method of the Date object to obtain the timestamp of the current date:
var timestamp = new Date().getTime(); console.log(timestamp);
The above code will output the current timestamp.
2. Convert the date into the specified format
In JavaScript, we can use the Date object method to format the date into the specified string format. Some common date formats are listed below:
The following is a sample code that uses JavaScript to convert a date into a specified format:
// 将日期格式化为yyyy-MM-dd格式 function formatDate(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); if (month < 10) month = "0" + month; if (day < 10) day = "0" + day; return year + "-" + month + "-" + day; } var date = new Date(); console.log(formatDate(date)); //2022-02-22 // 将日期格式化为MM/dd/yyyy格式 function formatDate2(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); if (month < 10) month = "0" + month; if (day < 10) day = "0" + day; return month + "/" + day + "/" + year; } console.log(formatDate2(date)); //02/22/2022 // 将日期格式化为dd-MM-yyyy格式 function formatDate3(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); if (month < 10) month = "0" + month; if (day < 10) day = "0" + day; return day + "-" + month + "-" + year; } console.log(formatDate3(date)); //22-02-2022 // 将日期格式化为yyyy-MM-dd HH:mm:ss格式 function formatDate4(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); if (month < 10) month = "0" + month; if (day < 10) day = "0" + day; if (hour < 10) hour = "0" + hour; if (minute < 10) minute = "0" + minute; if (second < 10) second = "0" + second; return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; } console.log(formatDate4(date)); //2022-02-22 00:00:00 // 将日期格式化为yyyyMMddHHmmss格式 function formatDate5(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); if (month < 10) month = "0" + month; if (day < 10) day = "0" + day; if (hour < 10) hour = "0" + hour; if (minute < 10) minute = "0" + minute; if (second < 10) second = "0" + second; return "" + year + month + day + hour + minute + second; } console.log(formatDate5(date)); //20220222000000
Using the above code, we can easily convert a date into a specified format.
Summary:
In web development, JavaScript is often used to change date formats. This article introduces how to convert dates into timestamps and convert dates into specified date formats. I hope it can help readers better understand JavaScript date processing.
The above is the detailed content of javascript change date format. For more information, please follow other related articles on the PHP Chinese website!