根据 时区 计算出 指定时间 在 该时区的 时间 ,parseInt的奇特用法
。
/** * * @param {*} time 时间戳 或者 YYYY/MM/DD hh:mm:ss格式的时间 * @param {*} timezone 客服设置的时区,可以是: "GMT+8" 或 8 * @param {*} cFormat 返回的时间格式 * @return 在当地显示客服设置的时区的时间 */ export function getCustomTimezoneTime(time, timezone, cFormat = '') { time = new Date(time).getTime() if (typeof time !== 'number' || isNaN(time)) return null timezone = timezone + '' let reg = /[+-]?\d+/, zone = timezone.match(reg), //客服时区,如 -6 customTime = parseInt(zone, 10) * 60 * 60 * 1000; // 客服时区时间差 let localTimezone = new Date().getTimezoneOffset(),//协调世界时(UTC)相对于当前时区的时间差值 单位(分钟) 注意:本地时间+这个差值=世界时间 localTime = localTimezone * 60 * 1000;//本体时间差 customTime = time + localTime + customTime;//这相当于指定时间的世界时间 return parseTime(customTime, cFormat) }
。
// 格式化时间 export function parseTime(time, cFormat) { if (arguments.length === 0) { return null } if (time === null || time === undefined) { return null } if (typeof new Date(time).getTime() !== 'number') { return null } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time = parseInt(time) } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { let value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return value } if (result.length > 0 && value < 10) { value = '0' + value } return value || 0 }) return time_str }
答疑:
'{y}-{m}-{d} {h}:{i}:{s}'.replace(/{(y|m|d|h|i|s|a)+}/g,(result,key) => { console.log("result: " + result + " key:" + key); })
parseInt的奇特用法:
console.log(parseInt(["+1"],10));// 1 console.log(parseInt(["-1"],10));// -1 console.log(parseInt(["+0"],10));// 0 console.log(parseInt(["-0"],10));// -0 console.log(parseInt("+0",10));// 0 console.log(parseInt("-0",10));// -0 console.log(parseInt("+8",10));// 8 console.log(parseInt("-8",10));// -8 console.log(parseInt(0-0,10));// 0
我好奇的是 他能接收 数组!!! 且数组的第一位才生效
。
分类:
javascript
, 平时所遇问题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通