Home > Web Front-end > JS Tutorial > JavaScript method toLocaleString() to convert a Date object into a string based on local time

JavaScript method toLocaleString() to convert a Date object into a string based on local time

黄舟
Release: 2017-11-08 14:06:33
Original
3755 people have browsed it

Definition and Usage

toLocaleString() method can convert the Date object to String according to the local time, and return the result .

Syntax

dateObject.toLocaleString()
Copy after login

Return value

A string representation of the dateObject, expressed in the local time zone and formatted according to local rules.

Example

Example 1

In this example, we will convert today’s date into a string according to local time:

<script type="text/javascript">

var d = new Date()
document.write(d.toLocaleString())

</script>
Copy after login

Output:

2017/11/8 下午2:06:15
Copy after login

Example 2

In this example, we will convert a specific date into a string according to local time:

<script type="text/javascript">

var born = new Date("July 21, 1983 01:15:00")
document.write(born.toLocaleString())

</script>
Copy after login

Output:

1983/7/21 上午1:15:00
Copy after login

How Use toLocaleString() to convert today's date to a string based on local time.

<html>
<body>

<script type="text/javascript">

var d = new Date()
document.write (d.toLocaleString())

</script>

</body>
</html>
Copy after login

Result:

2017/11/8 下午2:07:26
Copy after login

The above is the detailed content of JavaScript method toLocaleString() to convert a Date object into a string based on local time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template