forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.js
More file actions
104 lines (83 loc) · 3.25 KB
/
Copy pathdate.js
File metadata and controls
104 lines (83 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import store from '@/store'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
dayjs.extend(utc)
export function parseDayJsObject ({ value, format = true, keepMoment = true }) {
if (!value) {
return null
}
if (typeof value === 'string') {
value = dayjs(value)
}
if (!store.getters.usebrowsertimezone) {
value = value.utc(keepMoment)
}
if (!format) {
return value
}
return value.format()
}
/**
* When passing a string/dayjs to the date picker component, it converts the value to the browser timezone; therefore,
* we need to normalize the value to UTC if user is not using browser's timezone.
* @param {*} value The datetime to normalize.
* @returns A dayjs object with the datetime normalized to UTC if user is not using browser's timezone;
* otherwise, a correspondent dayjs object based on the value passed.
*/
export function parseDateToDatePicker (value) {
if (!value) {
return null
}
if (typeof value === 'string') {
value = dayjs(value)
}
if (store.getters.usebrowsertimezone) {
return value
}
return value.utc(false)
}
export function toLocalDate ({ date, timezoneoffset = store.getters.timezoneoffset, usebrowsertimezone = store.getters.usebrowsertimezone }) {
if (usebrowsertimezone) {
// Since GMT+530 is returned as -330 (minutes to GMT)
timezoneoffset = new Date().getTimezoneOffset() / -60
}
const milliseconds = Date.parse(date)
// e.g. "Tue, 08 Jun 2010 19:13:49 GMT"; "Tue, 25 May 2010 12:07:01 UTC"
return new Date(milliseconds + (timezoneoffset * 60 * 60 * 1000))
}
export function toLocaleDate ({ date, timezoneoffset = store.getters.timezoneoffset, usebrowsertimezone = store.getters.usebrowsertimezone, dateOnly = false, hourOnly = false }) {
if (!date) {
return null
}
let dateWithOffset = toLocalDate({ date, timezoneoffset, usebrowsertimezone }).toUTCString()
// e.g. "Mon, 03 Jun 2024 19:22:55 GMT" -> "03 Jun 2024 19:22:55 GMT"
dateWithOffset = dateWithOffset.substring(dateWithOffset.indexOf(', ') + 2)
// e.g. "03 Jun 2024 19:22:55 GMT" -> "03 Jun 2024 19:22:55"
dateWithOffset = dateWithOffset.substring(0, dateWithOffset.length - 4)
if (dateOnly) {
// e.g. "03 Jun 2024 19:22:55" -> "03 Jun 2024"
return dateWithOffset.substring(0, dateWithOffset.length - 9)
}
if (hourOnly) {
// e.g. "03 Jun 2024 19:22:55" -> "19:22:55"
return dateWithOffset.substring(dateWithOffset.length - 8, dateWithOffset.length)
}
return dateWithOffset
}
export { dayjs }