QML中的Date将时间戳和指定格式时间互转

在QML中,可以通过使用JavaScript来处理日期和时间的转换,其中包括将时间戳转换为指定格式的时间字符串,以及将时间字符串解析为时间戳的操作。

将时间戳转换为指定格式的时间字符串

在QML中,可以通过JavaScript的Date对象来处理时间戳的转换。

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    visible: true
    width: 400
    height: 200
    title: "时间戳与格式化时间转换示例"

    function timestampToFormattedString(timestamp, format) {
        var date = new Date(timestamp * 1000); // 注意JavaScript中的时间戳是以毫秒为单位的
        return Qt.formatDateTime(date, format);
    }

    // 示例用的时间戳
    property var timestampValue: 1628027245; // 例如,这里的时间戳对应日期:2021-08-04 09:20:45

    Text {
        id: timestampText
        anchors.centerIn: parent
        text: "时间戳:" + timestampValue
    }

    Text {
        anchors.top: timestampText.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        text: "格式化时间:" + timestampToFormattedString(timestampValue, "yyyy-MM-dd hh:mm:ss")
    }
}

解释

JavaScript函数 timestampToFormattedString:

timestampToFormattedString 函数将接受一个时间戳(单位为秒)和一个格式化字符串作为参数。
在函数内部,使用 new Date(timestamp * 1000) 将时间戳转换为JavaScript的Date对象。注意,在JavaScript中,时间戳是以毫秒为单位的,而在QML中通常是以秒为单位的,因此乘以1000来得到正确的毫秒时间戳。
使用 Qt.formatDateTime(date, format) 函数将Date对象格式化为指定的时间字符串。
timestampValue 属性定义了一个示例时间戳,这里使用了固定值,
Text 组件用于显示时间戳和转换后的格式化时间字符串。
将指定格式的时间字符串转换为时间戳
如果您需要将一个格式化的时间字符串转换为时间戳,您可以编写另一个JavaScript函数来实现这个功能。这通常涉及将时间字符串解析为Date对象,然后获取其对应的时间戳。

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    visible: true
    width: 400
    height: 200
    title: "格式化时间转换为时间戳示例"

    function formattedStringToTimestamp(formattedString, format) {
        var date = Qt.formatDateTime(formattedString, format);
        return date.getTime() / 1000; // 返回秒为单位的时间戳
    }

    // 示例用的格式化时间字符串
    property string formattedTimeString: "2021-08-04 09:20:45";

    Text {
        id: formattedText
        anchors.centerIn: parent
        text: "格式化时间:" + formattedTimeString
    }

    Text {
        anchors.top: formattedText.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        text: "时间戳:" + formattedStringToTimestamp(formattedTimeString, "yyyy-MM-dd hh:mm:ss")
    }
}

解释

JavaScript函数 formattedStringToTimestamp:

formattedStringToTimestamp 函数接受一个格式化的时间字符串和对应的格式化字符串作为参数。
使用 Qt.formatDateTime(formattedString, format) 函数将格式化的时间字符串转换为Date对象。
使用 date.getTime() 获取Date对象的时间戳(毫秒),然后将其转换为秒单位的时间戳。
formattedTimeString 属性定义了一个示例格式化的时间字符串,这里使用了固定值
Text 组件用于显示格式化的时间字符串和转换后的时间戳。

当使用 JavaScript 中的 Date 对象时,以下是一些常用的日期和时间相关方法的总结:

getDate(): 返回一个月中的某一天 (1 ~ 31)。
getDay(): 返回一周中的某一天 (0 ~ 6),其中 0 表示星期日。
getMonth(): 返回月份 (0 ~ 11),其中 0 表示一月。
getFullYear(): 返回四位数字的年份。
getHours(): 返回小时 (0 ~ 23)。
getMinutes(): 返回分钟 (0 ~ 59)。
getSeconds(): 返回秒数 (0 ~ 59)。
getMilliseconds(): 返回毫秒数 (0 ~ 999)。
getTime(): 返回自 1970 年 1 月 1 日 00:00:00 UTC 起的毫秒数。
toString(): 将 Date 对象转换为字符串,返回包含完整日期和时间的字符串。
toTimeString(): 将 Date 对象的时间部分转换为字符串,返回时间字符串。
toDateString(): 将 Date 对象的日期部分转换为字符串,返回日期字符串。
toLocaleString(): 根据本地时间格式,将 Date 对象转换为字符串。
toLocaleTimeString(): 根据本地时间格式,将 Date 对象的时间部分转换为字符串。
toLocaleDateString(): 根据本地时间格式,将 Date 对象的日期部分转换为字符串。
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值