Home > Web Front-end > JS Tutorial > body text

How to Parse a Date String in dd.mm.yyyy Format in JavaScript?

DDD
Release: 2024-10-31 20:15:29
Original
1052 people have browsed it

How to Parse a Date String in dd.mm.yyyy Format in JavaScript?

Parsing Date Strings in JavaScript in dd.mm.yyyy Format

In JavaScript, parsing a date string in the format dd.mm.yyyy is a simple process that involves splitting the string into its component parts and creating a Date object.

To achieve this, the following steps should be taken:

  • Split the Date String: Divide the date string into an array of three elements (day, month, year) using the split() method, with a dot as the separator.
  • Create a Date Object: Use the Date constructor to create a Date object, passing in the year as the first argument, the zero-indexed month (i.e., month - 1) as the second argument, and the day as the third argument.

Example Code:

<code class="javascript">var strDate = "03.09.1979";
var dateParts = strDate.split(".");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);</code>
Copy after login

This code splits the date string "03.09.1979" into ["03", "09", "1979"] and creates a Date object with the corresponding values. The resulting date variable will represent the date September 3, 1979.

The above is the detailed content of How to Parse a Date String in dd.mm.yyyy Format in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

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