問題源于antd 4.0之後我們使用了day.js代替了moment.js.在設定了阿拉伯語的情況下,後端傳回的時間日期是"2020/٠٧/٣١"以及"١٤:٠٦", TimePicker會出錯無法修改時間。
在不想重新啟用moment.js也不想引入語言包的情況下。我們可以使用toLocaleString()
MDN指路
如果使用moment.js的話 直接moment.locale('ar')就好了
使用toLocaleString()的話代碼如下
let values = {
cur_locale: "ar",
cur_time: "١٤:٣٠",
cur_date: "2020/٠٧/٣١",
} //提供資料 供測試
let {locale, cur_time, cur_date} = values
let speciaLocale = ['ar'] // ar 阿拉伯語 特殊語言 傳回值非“0123”這種數字 需要轉化
if (speciaLocale.includes(locale)) {
let comArr = []
comArr.length = 10
comArr.fill(0) // 快速填充數組
comArr.map((item, i) => {
comArr[i] = i.toLocaleString(locale) // 下标與對應語言的數字對應
})
// comArr 為["٩", "٨", "٧", "٦", "٥", "٤", "٣", "٢", "١", "٠"] 對應從0-9
cur_time = this.transTime(cur_time, comArr)
cur_date = this.transTime(cur_date, comArr)
}
transTime = (timeStr, comArr) => {
let time = ''
timeStr.split('').map(item => {
let index = comArr.indexOf(item)
if (index !== -1) {
time = time + index // index即對應的阿拉伯數字
} else {
time = time + item
}
})
return time
}