Skip to content

formatDate - regexp pitfall #7

Closed
@grazumkov

Description

@grazumkov
var df = new DateFormatter();
// Suppose now time in 24H is '21:30'
var firstCall = df.formatDate(new Date(), "H:i"); // "21:30"
var secondCall= df.formatDate(new Date(), "H:i"); // "H:30"
var thirdCall= df.formatDate(new Date(), "H:i"); // "H:30"
var forthCall= df.formatDate(new Date(), "H:i"); // "H:30"

Problem is regexp flag 'g'.
For, example - 'validParts' option (/[dDjlNSwzWFmMntLoYyaABgGhHisueTIOPZcrU]/g).

options.validParts.test('H') // true
options.validParts.test('H') // false
options.validParts.test('H') // true
options.validParts.test('H') // false

Because a call regular expression with
the global flag set will start matching from the regexp's current
value of lastIndex - the index of the previous match.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions