相关文章推荐
眉毛粗的油条  ·  jQuery键盘事件详解·  1 月前    · 
正直的香瓜  ·  获取QTreeView的某一条目的所有数据_ ...·  1 年前    · 
傻傻的楼梯  ·  Excel条件格式中数据条、色阶、图标集使用 ...·  1 年前    · 
气宇轩昂的热水瓶  ·  「效率」使用VScode连接远程服务器进行开 ...·  2 年前    · 
性感的钥匙扣  ·  Python - 常⽤的操作 excel ...·  2 年前    · 
Code  ›  Js/JQuery根据时区获取当前时间开发者社区
js js代码 jquery
https://cloud.tencent.com/developer/article/2234305
小胡子的麦片
2 年前
作者头像
目的地-Destination
0 篇文章

Js/JQuery根据时区获取当前时间

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 目的地-Destination > Js/JQuery根据时区获取当前时间

Js/JQuery根据时区获取当前时间

作者头像
目的地-Destination
发布 于 2023-03-06 16:24:50
653 0
发布 于 2023-03-06 16:24:50
举报

javascript代码

function getDate(timezone) {
    timezone; //目标时区时间,东八区
    var offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差,单位为分钟
    var nowDate = new Date().getTime(); // 本地时间距 1970 年 1 月 1 日午夜(GMT 时间)之间的毫秒数
    var targetDate = new Date(nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000);
    return targetDate;
}

调用方法

getDate(-6);
getDate(8);
getDate(9);

显示结果

当前时间:Mon Dec 12 2022 22:39:53
时区为-6的时间:Mon Dec 12 2022 08:39:53
时区为+8的时间:Mon Dec 12 2022 22:39:53
时区为+9的时间:Mon Dec 12 2022 23:39:53

完整Demo

<!doctype html>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
    function getDate(timezone) {
        timezone; //目标时区时间,东八区
        var offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差,单位为分钟
        var nowDate = new Date().getTime(); // 本地时间距 1970 年 1 月 1 日午夜(GMT 时间)之间的毫秒数
        var targetDate = new Date(nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000);
        return targetDate;
$(function(){ 
    $("#div1").text(getDate(-6));
    $("#div2").text(getDate(8));
    $("#div3").text(getDate(9));
window.οnlοad=function(){
    var userName="后端码匠";
    alert(userName);
</script>
</head>
 
推荐文章
眉毛粗的油条  ·  jQuery键盘事件详解
1 月前
正直的香瓜  ·  获取QTreeView的某一条目的所有数据_qt 获取值 treeview 所有值-CSDN博客
1 年前
傻傻的楼梯  ·  Excel条件格式中数据条、色阶、图标集使用方法_哔哩哔哩_bilibili
1 年前
气宇轩昂的热水瓶  ·  「效率」使用VScode连接远程服务器进行开发 - 知乎
2 年前
性感的钥匙扣  ·  Python - 常⽤的操作 excel 第三方库-阿里云开发者社区
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号