import QtQuick import QtQuick.Controls import QtQuick.Layouts Item { width: 800 height: 30 property var text: "" property var value: "" property var readonly: false property var currentDate: new Date() // 获取当前时间 clip: true Row{ spacing: 20 ComboBox{ id: cb width: 100 height: 30 model: ["无", "结束时间", "相对时间"] onCurrentIndexChanged:{ if (cb.currentIndex === 0) { g_stopTime = null } } } Row { spacing: 5 visible: cb.currentIndex === 1 SetSpinBox { id: yearSpinBox from: 1900 to: 9999 value: currentDate.getFullYear() // 年份 onValueChanged: { updateDaySpinBox() updateStopTime() } } SetSpinBox { id: monthSpinBox from: 1 to: 12 value: currentDate.getMonth() + 1 // 月份 onValueChanged: { updateDaySpinBox() updateStopTime() } } SetSpinBox { id: daySpinBox from: 1 to: 31 value: currentDate.getDate() // 日 onValueChanged: { updateStopTime() } } SetSpinBox { id: hourSpinBox from: 0 to: 23 value: currentDate.getHours() // 小时 onValueChanged: { updateStopTime() } } SetSpinBox { id: minSpinBox from: 0 to: 59 value: currentDate.getMinutes() // 分钟 onValueChanged: { updateStopTime() } } SetSpinBox { id: secSpinBox from: 0 to: 59 value: currentDate.getSeconds() // 秒 onValueChanged: { updateStopTime() } } } TextField { id: unitTextField visible: cb.currentIndex == 2 width: 200 height: 30 font.pointSize: 12 placeholderText: "1d 2h 30m 40s" inputMethodHints: Qt.ImhDigitsOnly onEditingFinished: { // var pattern = /^(\d+d\s*)*(\d+h\s*)*(\d+m\s*)*(\d+s\s*)*$/; // if (!pattern.test(text)) { // text = text.substring(0, text.length - 1); // } updateStopTime() } } } function updateDaySpinBox() { var year = yearSpinBox.value var month = monthSpinBox.value var maxDay = 31 if (month === 4 || month === 6 || month === 9 || month === 11) { maxDay = 30 } else if (month === 2) { if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) { maxDay = 29 } else { maxDay = 28 } } daySpinBox.to = maxDay } function updateStopTime(){ if (cb.currentIndex === 1) { // 日期时间 var year = yearSpinBox.value var month = monthSpinBox.value-1 // 月份从0开始计数,所以需要减1 var day = daySpinBox.value var hour = hourSpinBox.value var minute = minSpinBox.value var second = secSpinBox.value g_stopTime = new Date(year, month, day, hour, minute, second) } else if (cb.currentIndex === 2) { // 相对时间 var input = unitTextField.text var timestamp = Date.now(); // Get current timestamp in milliseconds var pattern = /(\d+)([dhms])/g; var match; while ((match = pattern.exec(input)) !== null) { var value = parseInt(match[1]); var unit = match[2]; if (unit === 'd') { timestamp += value * 24 * 60 * 60 * 1000; // Convert days to milliseconds } else if (unit === 'h') { timestamp += value * 60 * 60 * 1000; // Convert hours to milliseconds } else if (unit === 'm') { timestamp += value * 60 * 1000; // Convert minutes to milliseconds } else if (unit === 's') { timestamp += value * 1000; // Convert seconds to milliseconds } } g_stopTime = new Date(timestamp); // Convert timestamp to Date object } console.info(g_stopTime) } }