QML⼯作笔记-使⽤QML中的Date将时间戳和指定格式时间互
⽬录
背景
这个功能⾮常有⽤,经常⽤到,今天查了⼤半个⼩时的⽂档才弄出来,特意记录下,⽅便以前进⾏快速查阅,开发。
代码及演⽰
程序运⾏截图如下:
unix时间戳转换日期格式
源码如下:
import QtQuick 2.9
import QtQml 2.2
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MouseArea{
anchors.fill: parent
onClicked: {
var currentDate = new Date()
//将本地时间转成指定的格式
console.LocaleString(Qt.locale("de_DE"), "yyyy-MM-dd HH:mm:ss"))
//将本地时间转成时间戳
var value = Time();
console.log(value)
//将时间戳转为指定格式
var ptDate = new Date(value)
console.LocaleString(Qt.locale("de_DE"), "yyyy-MM-dd HH:mm:ss"))
console.log("-------------华丽的分割线-------------")
}
}
}
补充
⽇期Date
getDate(): 从Date对象返回⼀个⽉中的某⼀天 (1 ~ 31)。
getDay(): 从Date对象返回⼀周中的某⼀天 (0 ~ 6)。
getMonth(): 从Date对象返回⽉份 (0 ~ 11)。
getFullYear(): 从Date对象以四位数字返回年份。
getHours(): 返回Date对象的⼩时 (0 ~ 23)。
getMinutes(): 返回Date对象的分钟 (0 ~ 59)。
getSeconds(): 返回Date对象的秒数 (0 ~ 59)。
getMilliseconds(): 返回Date对象的毫秒(0 ~ 999)。
getTime(): 返回1970年1⽉1⽇⾄今的毫秒数。
toString(): 把Date对象转换为字符串。
toTimeString(): 把Date对象的时间部分转换为字符串。
toDateString(): 把Date对象的⽇期部分转换为字符串。
toLocaleString(): 根据本地时间格式,把Date对象转换为字符串。toLocaleTimeString(): 根据本地时间格式,把Date对象的时间部分转换为字符串。toLocaleDateString(): 把Date对象的⽇期部分转换为字符串。