Home > Article > Web Front-end > How to convert date and time representations in JavaScript
JavaScript date and time expressions are very diverse, such as ISO format, standard date format, timestamp format, etc. In JavaScript, we often need to convert between these different representations to meet different needs.
This article will introduce how to convert date and time expressions in JavaScript, covering the following aspects:
In computers, Datetime is a data type used to represent time, with attributes such as year, month, day, hour, minute, and second. There are many forms of expression of date and time, and different expression forms can meet different needs. For example, we usually use "year-month-day" to represent a date, and "hour:minute:second" to represent a time, and timestamps are often used in databases to represent a point in time.
In JavaScript, the date and time object is a built-in object that can be used to represent a date or time. We can use the Date constructor to create a date and time object, for example:
var now = new Date(); // 获取当前时间的日期时间对象 var date = new Date("2022-01-01"); // 根据日期字符串创建日期时间对象 var time = new Date(1641004800000); // 根据时间戳创建日期时间对象
As you can see, using different parameters, we can create different date and time objects. In JavaScript, date and time objects have many built-in methods that can be used to get or set various attributes of date and time, for example:
var now = new Date(); var year = now.getFullYear(); // 获取当前年份 var month = now.getMonth() + 1; // 获取当前月份 var day = now.getDate(); // 获取当前日期 var hour = now.getHours(); // 获取当前小时数 var minute = now.getMinutes(); // 获取当前分钟数 var second = now.getSeconds(); // 获取当前秒数 var timestamp = now.getTime(); // 获取当前时间的时间戳
Commonly used in JavaScript The date and time expression forms are:
ISO date and time format
ISO format is an international standard date and time format, with the following form: "YYYY-MM- DDTHH:mm:ss.sssZ". Among them, YYYY represents the year, MM represents the month, DD represents the date, HH represents the hour, mm represents the minute, ss represents the number of seconds, and sss represents the number of milliseconds. T represents the date and time separator, Z represents the time zone.
Standard date format
The standard date format is a common date format that has the following form: "YYYY/MM/DD". Among them, YYYY represents the year, MM represents the month, and DD represents the date. The standard time format has the following form: "HH:mm:ss". Among them, HH represents hours, mm represents minutes, and ss represents seconds. If you want to represent both date and time, you can separate them with spaces, for example: "YYYY/MM/DD HH:mm:ss".
Timestamp format
A timestamp is a number of seconds or milliseconds calculated from a fixed point in time, usually expressed in integer form.
For a date and time object, we can use toISOString() method to convert it to an ISO format string, for example:
var now = new Date(); var isoString = now.toISOString(); // 输出的结果为:2022-08-22T01:17:25.753Z
For a date and time object, we can use the toLocaleDateString() method to convert it Convert to a string in standard date format, for example:
var now = new Date(); var dateString = now.toLocaleDateString(); // 输出的结果为:2022/8/22 var timeString = now.toLocaleTimeString(); // 输出的结果为:上午1:17:25 var dateTimeString = now.toLocaleString(); // 输出的结果为:2022/8/22 上午1:17:25
For a date and time object, we can use the getTime() method to convert it to Timestamp, for example:
var now = new Date(); var timestamp = now.getTime(); // 输出的结果为:1669091841918
For an ISO format date time string, we can use the new Date() constructor to convert it to Date and time object, for example:
var isoString = "2022-08-22T01:17:25.753Z"; var date = new Date(isoString); // 输出的结果为:Mon Aug 22 2022 09:17:25 GMT+0800 (中国标准时间)
For a string in standard date format, "year/month/day" can use String's replace () method replaces "/" with "-", for example:
var dateString = "2022/8/22"; dateString = dateString.replace(/\//g, "-"); // 输出的结果为:2022-08-22 var date = new Date(dateString); // 输出的结果为:Mon Aug 22 2022 00:00:00 GMT+0800 (中国标准时间)
For a timestamp, we can pass it as a parameter to Date constructor, for example:
var timestamp = 1669091841918; var date = new Date(timestamp); // 输出的结果为:Mon Aug 22 2022 01:57:21 GMT+0800 (中国标准时间)
In summary, this article introduces the basic concepts of date and time in JavaScript, date and time objects, date and time expressions, and conversion methods between different expressions, and gives Specific example analysis. Different expression forms can meet different needs. For example: ISO format is suitable for use in network transmission, standard date format is suitable for use in human-computer interaction, and timestamp format is suitable for time calculation and comparison. Therefore, in actual development, we need to choose a suitable date and time representation and conversion method according to specific needs.
The above is the detailed content of How to convert date and time representations in JavaScript. For more information, please follow other related articles on the PHP Chinese website!