您好,我需要呼叫一個 API 并傳遞一個日期引數,后端已指定它必須是 ISO 8601。這是他們給我的示例: 2020-01-01T00:00:00+01:00
the : is the escape character for :
the + is the escape character for
如果我這樣做,console.log(new Date().toISOString())我就會得到2021-10-12T13:13:27.633Z
當我將它轉換為 ISOString() 時,api 失敗。
我能做什么?
uj5u.com熱心網友回復:
我解決了。我是這樣做的:
toIsoStringCustom(date: Date) {
var tzo = -date.getTimezoneOffset(),
dif = tzo >= 0 ? ' ' : '-',
pad = function(num: number) {
var norm = Math.floor(Math.abs(num));
return (norm < 10 ? '0' : '') norm;
};
return date.getFullYear()
'-' pad(date.getMonth() 1)
'-' pad(date.getDate())
'T' pad(date.getHours())
':' pad(date.getMinutes())
':' pad(date.getSeconds())
dif pad(tzo / 60)
':' pad(tzo % 60);
}
let today = new Date()
console.log(encodeURIComponent(this.toIsoStringCustom(today)))
uj5u.com熱心網友回復:
您要實作的目標與日期格式無關。它只是這樣編碼。encodeURIComponent() 函式通過用一個、兩個、三個或四個表示字符的 UTF-8 編碼的轉義序列替換某些字符的每個實體來對 URI 進行編碼(對于由兩個“代理”組成的字符,只有四個轉義序列)人物)。
檢查下面的這個片段:
console.log(encodeURIComponent("2021-10-12T13:13:27.633Z"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/310064.html
標籤:javascript 日期 等值线
上一篇:獲取html屬性值
