Home  >  Article  >  Web Front-end  >  How to Parse a Date String in the Format dd.mm.yyyy in JavaScript?

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

Barbara Streisand
Barbara StreisandOriginal
2024-10-28 03:39:30564browse

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

Parse Date String in JavaScript

Parsing date strings into a required format is often encountered in JavaScript development. Specifically, when dealing with dates in the format dd.mm.yyyy, a clear and efficient approach is necessary.

Solution

To parse a date string in the format dd.mm.yyyy, you can leverage the following steps:

  1. Split the Date String: Utilize the String.Split method to separate the date string into its components (day, month, and year) based on the '.' separator.
  2. Create a Date Object: Construct a new Date object using the values extracted from the previous step, ensuring that the month index is adjusted by 1 (as JavaScript uses 0-based month indexing).

Example Code

<code class="js">var strDate = "03.09.1979";
var dateParts = strDate.split(".");

var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);</code>

By following this approach, you can effectively parse date strings in the required format dd.mm.yyyy in JavaScript.

The above is the detailed content of How to Parse a Date String in the Format dd.mm.yyyy 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