Skip to content

Commit c69f108

Browse files
committed
refactor(CDateRangePicker): move utilities to a separate file
1 parent db20047 commit c69f108

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

js/src/date-range-picker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import EventHandler from './dom/event-handler.js'
1313
import Manipulator from './dom/manipulator.js'
1414
import SelectorEngine from './dom/selector-engine.js'
1515
import { defineJQueryPlugin, getElement, isRTL } from './util/index.js'
16-
import { convertToDateObject, getLocalDateFromString } from './util/calendar.js'
16+
import { convertToDateObject } from './util/calendar.js'
17+
import { getLocalDateFromString } from './util/date-range-picker.js'
1718

1819
/**
1920
* Constants

js/src/util/date-range-picker.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const getLocalDateFromString = (string, locale, time) => {
2+
const date = new Date(2013, 11, 31, 17, 19, 22)
3+
let regex = time ? date.toLocaleString(locale) : date.toLocaleDateString(locale)
4+
regex = regex
5+
.replace('2013', '(?<year>[0-9]{2,4})')
6+
.replace('12', '(?<month>[0-9]{1,2})')
7+
.replace('31', '(?<day>[0-9]{1,2})')
8+
if (time) {
9+
regex = regex
10+
.replace('5', '(?<hour>[0-9]{1,2})')
11+
.replace('17', '(?<hour>[0-9]{1,2})')
12+
.replace('19', '(?<minute>[0-9]{1,2})')
13+
.replace('22', '(?<second>[0-9]{1,2})')
14+
.replace('PM', '(?<ampm>[A-Z]{2})')
15+
}
16+
17+
const rgx = new RegExp(`${regex}`)
18+
const partials = string.match(rgx)
19+
if (partials === null) {
20+
return
21+
}
22+
23+
const newDate = partials.groups &&
24+
(time ?
25+
new Date(Number(partials.groups.year, 10), Number(partials.groups.month, 10) - 1, Number(partials.groups.day), partials.groups.ampm ?
26+
(partials.groups.ampm === 'PM' ?
27+
Number(partials.groups.hour) + 12 :
28+
Number(partials.groups.hour)) :
29+
Number(partials.groups.hour), Number(partials.groups.minute), Number(partials.groups.second)) :
30+
new Date(Number(partials.groups.year), Number(partials.groups.month) - 1, Number(partials.groups.day)))
31+
return newDate
32+
}

0 commit comments

Comments
 (0)