Qml----webengine打开的页面根据系统时区修改,javascript同理

注意webengine只有msvc编译环境才能用

main.qml

c++ 复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtWebEngine 1.9


Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    WebEngineView{
        anchors.fill:parent;
        url:"./test.html"
    }
}

test.html

复制代码
<!DOCTYPE html>
<html lang ="zh-CN">
<head>test
<meta charset="UTF-8">

</head>
<script>
function updateTime(){
    var now = new Date();
    var timestamp = 1609459200000; //2021-01-01 00.00.00 utc
    var localTime = new Date(timestamp);


    //跟新当前时间
    document.getElementById('current-time').textContent = unChangeTimezone(now);

    //跟新时间戳
    document.getElementById('timestamp-time').textContent = unChangeTimezone(localTime);

    //跟新修改的当前时间
    document.getElementById('change-current-time').textContent = ChangeTImezone(now).toString();

    //跟新修改的时间戳
    document.getElementById('change-timestamp-time').textContent = ChangeTImezone(timestamp).toString();
}

window.onload = function(){
    setInterval(updateTime,1000);
    updateTime();
}
function unChangeTimezone(n){
    var now = new Date(n)
    y = now.getFullYear();
    m = now.getMonth()+1;
    d = now.getDate();
    return y + "-" + (m<10? "0"+m : m) + "-" + (d <10 ? "0"+d:d) + " " +now.toTimeString().substr(0,8);
}

function ChangeTImezone(datevalue){

    //获取本地时区
    timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    document.getElementById('timezone').textContent = timezone;

    const options = {
        year:'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12:false,
        timeZone:timezone
    };
   
    const parts = new Intl.DateTimeFormat('zh-CN',options).formatToParts(datevalue);
    // if(datevalue instanceof Date && !isNaN(datevalue.getTime())){
    //     //有效Date对象
    //     const parts = formatter.formatToParts(datevalue);
    // }
    // else if(typeof datevalue === 'number' && !isNaN(datevalue)){
    //     //有效时间戳
       
    //     const parts = formatter.formatToParts(new Date(datevalue));
    // }

    // 手动拼接成 'YYYY-MM-DD HH:MM:SS' 格式
    const year = parts.find(part => part.type === 'year').value;
    const month = parts.find(part => part.type === 'month').value;
    const day = parts.find(part => part.type === 'day').value;
    const hour = parts.find(part => part.type === 'hour').value;
    const minute = parts.find(part => part.type === 'minute').value;
    const second = parts.find(part => part.type === 'second').value;
    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}

</script>
<body>
    <p>当前时间:<span id = "current-time"></span></p>
    <p>时间戳时间(utc2021-01-01 00.00.00):<span id = "timestamp-time"></span></p>
    <p>修改后的当前时间:<span id = "change-current-time"></span></p>
    <p>修改后时间戳时间(北京时间应该为2021.01.01 08):<span id = "change-timestamp-time"></span></p>
    <p>显示时区 <spad id = "timezone"></spad></p>
</body>
</html>​

手动修改电脑的时区,网页上也会修改

相关推荐
名字还没想好☜6 分钟前
Go 用 bufio.Scanner 读大文件踩坑:默认 64KB 行上限、Buffer 扩容与按 Token 切分
开发语言·后端·golang·go
天天鸭1 小时前
5 万处中文的老项目实现国际化,如何用架构思维完成改造?
前端·javascript·架构
Byron Loong1 小时前
【C++】重定向是什么
开发语言·c++
techdashen2 小时前
Go设计取舍之六: sync.Mutex正常模式与饥饿模式
开发语言·后端·golang
TunerT_TQ2 小时前
【智能体安全治理|专栏第9期】从“一堆规则”到“数字宪法”:智能体治理的下一个阶段
java·开发语言·安全·开源治理·大模型安全·ai基础设施·智能体安全
Aurorar0rua2 小时前
CS50 x 2024 Notes Algorithms - 07
c语言·开发语言·学习方法
月月大王的3D日记2 小时前
Three.js 入门系列(7):让甜甜圈围着我转 —— 3D文字生成
前端·javascript
程序员-Benothing2 小时前
Java ForkJoinPool 详解:从分治思想到高性能并行计算
java·开发语言·后端·面试·职场和发展
脚踏实地皮皮晨3 小时前
003006001_WrapPanel控件
开发语言·c#·.net·wpf·visual studio
上海安当技术3 小时前
单点登录 SSO 怎么选协议?SAML 2.0 / OAuth 2.0 / OIDC / CAS 对比与 ERP、OA、CRM 接入实战
java·开发语言