Home  >  Article  >  Web Front-end  >  How to convert date to string in javascript

How to convert date to string in javascript

PHPz
PHPzOriginal
2023-04-24 15:48:426048browse

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.

  1. Date object

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.

  1. Convert Date object into string

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()

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"
  • toLocaleDateString()

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"
  • toGMTString()

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"
  • toISOString()

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()

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 (中国标准时间)"
  1. Format date string

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:

  • Get the year, month and day
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"
  • Get the hours, minutes and seconds
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"
  • Get the week
var date = new Date();
var week = ['日', '一', '二', '三', '四', '五', '六'];
var str = '星期' + week[date.getDay()];
console.log(str) // 输出: "星期一"
  • Custom format
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn