In JavaScript, we often need to convert dates into string format for display or transmission to the server. Date objects represent time and date information in JavaScript, while strings are a textual representation. This article will explore how to convert dates into string format, as well as some common formatting methods.
First we need to understand the Date object. The Date object is an object used in JavaScript for working with dates and times. We can use the Date object to obtain various time units such as year, month, date, hour, minute, second, etc. The syntax for creating a Date object is as follows:
var date = new Date();
If we want to create a Date object for a specific date, we can use the following syntax:
var date = new Date(year, month, day, hours, minutes, seconds, milliseconds);
Among them, year, month and day are required. , represents the date; hours, minutes, seconds and milliseconds are optional and represent hours, minutes, seconds and milliseconds.
To convert Date object into string, we can use toDateString(), toLocaleDateString(), toGMTString provided by Date object (), toISOString(), toString() and other methods. Below we introduce these methods respectively:
toDateString() method converts the date part of the Date object into a string. For example:
var date = new Date(); var str = date.toDateString(); console.log(str) // 输出: "Mon May 24 2021"
The toLocaleDateString() method converts the date part of the Date object into a localized date string. For example:
var date = new Date(); var str = date.toLocaleDateString(); console.log(str) // 输出: "2021/5/24"
The toGMTString() method converts the date part and Greenwich Mean Time of a Date object into a string. For example:
var date = new Date(); var str = date.toGMTString(); console.log(str) // 输出: "Mon, 24 May 2021 08:13:20 GMT"
The toISOString() method converts a Date object into an ISO format string. For example:
var date = new Date(); var str = date.toISOString(); console.log(str) // 输出: "2021-05-24T08:13:20.321Z"
toString() method converts a Date object into a string. For example:
var date = new Date(); var str = date.toString(); console.log(str) // 输出: "Mon May 24 2021 16:13:20 GMT+0800 (中国标准时间)"
The string format returned in the above method does not necessarily meet our needs. To do this, we can use various formatting methods to format the date into the string format we need. The following are some commonly used formatting methods:
var date = new Date(); var year = date.getFullYear(); // 获取年份 var month = date.getMonth() + 1; // 获取月份 var day = date.getDate(); // 获取日期 var str = year + '-' + month + '-' + day; console.log(str) // 输出: "2021-5-24"
var date = new Date(); var hours = date.getHours(); // 获取小时 var minutes = date.getMinutes(); // 获取分钟 var seconds = date.getSeconds(); // 获取秒数 var str = hours + ':' + minutes + ':' + seconds; console.log(str) // 输出: "16:36:3"
var date = new Date(); var week = ['日', '一', '二', '三', '四', '五', '六']; var str = '星期' + week[date.getDay()]; console.log(str) // 输出: "星期一"
var date = new Date(); var year = date.getFullYear(); // 获取年份 var month = date.getMonth() + 1; // 获取月份 var day = date.getDate(); // 获取日期 var hours = date.getHours(); // 获取小时 var minutes = date.getMinutes(); // 获取分钟 var seconds = date.getSeconds(); // 获取秒数 var str = year + '-' + formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hours) + ':' + formatNumber(minutes) + ':' + formatNumber(seconds); console.log(str) // 输出: "2021-05-24 16:37:45" function formatNumber(n) { n = n.toString(); return n[1] ? n : '0' + n; }
The above methods are just some of the more commonly used formatting methods. In fact, we can also customize various formats date string.
In short, converting date objects into string format is an operation we often use. By understanding the Date object and various formatting methods, we can easily format various date strings.
The above is the detailed content of How to convert date to string in javascript. For more information, please follow other related articles on the PHP Chinese website!