Created
July 29, 2023 13:20
-
-
Save wmakeev/634ae6225c530f6bd98dc60a5b9346c1 to your computer and use it in GitHub Desktop.
[Яндекс Маркет (даты)] #yandex #market #date #parse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import assert from 'node:assert' | |
const YA_DATE_REGEX = /(\d\d)-(\d\d)-(\d\d\d\d)(?:\s(\d\d):(\d\d):(\d\d))?/g | |
/** | |
* Разбор даты в формате `ДД-ММ-ГГГГ ЧЧ:ММ:СС` | |
* @param {string} date | |
*/ | |
export const parseYaMarketDate = date => { | |
YA_DATE_REGEX.lastIndex = 0 | |
const dateMatch = YA_DATE_REGEX.exec(date) | |
assert.ok(dateMatch, 'Некорректный формат даты') | |
const [, day, month, year, hour = '00', minute = '00', second = '00'] = | |
Array.from(dateMatch) | |
const parsedDate = new Date( | |
`${year}-${month}-${day}T${hour}:${minute}:${second}+03:00` | |
) | |
return parsedDate | |
} | |
// console.log(parseYaMarketDate('27-07-2023')) | |
// console.log(parseYaMarketDate('24-07-2023 16:34:44')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment