-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathgetServerTime.js
More file actions
35 lines (31 loc) · 1.16 KB
/
getServerTime.js
File metadata and controls
35 lines (31 loc) · 1.16 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
import ajax from './ajax.js'
import getURL from './getURL.js'
/**
@name $SP().getServerTime
@function
@category utils
@description Return the current server time (or convert the passed date)
@param {Date} [date=new Date()] You can send a Date object and it will be converted using the server time
@param {String} [url="current website"] The url of the website
@return {Promise} resolve(DateInUTC), reject(error)
@example
$SP().getServerTime().then(function(dateInUTC) {
console.log("Server Time: "+dateInUTC);
}, function(error) {
console.log(error)
})
*/
export default async function getServerTime(date, url) {
try {
date = date || new Date();
// find the base URL
if (!url) {
url = await getURL.call(this);
}
let data = await ajax.call(this, {url:url + "/_api/web/RegionalSettings/TimeZone/utcToLocalTime(@date)?@date='"+ date.toUTCString() + "'"});
if (typeof data === "object" && data.d && data.d.UTCToLocalTime) return Promise.resolve(data.d.UTCToLocalTime+'Z');
return Promise.reject("[getServerTime] The server didn't return the expected response.");
} catch(err) {
return Promise.reject(err);
}
}