时间差计算工具(计算两个日期、时刻之间的时间差)和 源码实现
目录1 时间的输入方式2 时间差计算3 源码实现方式4 源码和软件下载1 时间的输入方式输入时间有两种方式手动直接输入年月日期和时间通过时间选择器选择年月日通过上下箭头修改时间时间选择器选择年月日2 时间差计算时间差计算要求开始时间必须早于结束时间否则提示报错此时可以通过点击交换时间按钮交换开始与结束的时间。3 源码实现方式源码实现APP_STYLE QWidget { color: #172033; font-family: Microsoft YaHei UI, Segoe UI; font-size: 14px; } QMainWindow, QWidget#centralWidget { background: #f3f6fb; } QLabel#title { color: #111827; font-size: 28px; font-weight: 700; } QLabel#subtitle { color: #667085; font-size: 13px; } QFrame#card, QFrame#resultCard { background: #ffffff; border: 1px solid #e7ebf2; border-radius: 16px; } QFrame#resultCard[statuserror] { background: #fff8f7; border-color: #fecaca; } QLabel#sectionTitle { color: #344054; font-size: 16px; font-weight: 700; } QLabel#fieldLabel { color: #667085; font-size: 12px; font-weight: 600; } QDateEdit, QTimeEdit { background: #f8fafc; border: 1px solid #dbe2ea; border-radius: 10px; padding: 10px 12px; font-size: 15px; selection-background-color: #2563eb; } QComboBox { min-height: 38px; padding: 0 12px; background: #f8fafc; border: 1px solid #dbe2ea; border-radius: 9px; } QComboBox:hover, QComboBox:focus { border-color: #3b82f6; } QComboBox::drop-down { width: 28px; border: 0; } QComboBox QAbstractItemView { background: #ffffff; border: 1px solid #dbe2ea; selection-background-color: #eff6ff; selection-color: #1d4ed8; padding: 4px; } QDateEdit:hover, QTimeEdit:hover { border-color: #94a3b8; } QDateEdit:focus, QTimeEdit:focus { background: #ffffff; border: 2px solid #3b82f6; padding: 9px 11px; } QDateEdit::drop-down { width: 30px; border: 0; border-left: 1px solid #e2e8f0; } QPushButton { min-height: 40px; padding: 0 18px; border-radius: 10px; font-weight: 600; } QPushButton#primaryButton { color: #ffffff; background: #2563eb; border: 1px solid #2563eb; font-size: 15px; min-height: 46px; } QPushButton#primaryButton:hover { background: #1d4ed8; border-color: #1d4ed8; } QPushButton#primaryButton:pressed { background: #1e40af; } QPushButton#secondaryButton { color: #344054; background: #ffffff; border: 1px solid #dbe2ea; } QPushButton#secondaryButton:hover { background: #f8fafc; border-color: #aab5c4; } QPushButton#textButton { color: #2563eb; background: transparent; border: 0; padding: 0 8px; min-height: 34px; } QPushButton#textButton:hover { background: #eff6ff; } QLabel#resultHeading { color: #667085; font-size: 12px; font-weight: 700; } QLabel#calendarResult { color: #172033; font-size: 22px; font-weight: 700; } QLabel#elapsedResult { color: #475467; font-size: 14px; } QLabel#errorResult { color: #b42318; font-size: 15px; font-weight: 600; } QLabel#hint { color: #98a2b3; font-size: 12px; } QCalendarWidget QWidget { background: #ffffff; } QCalendarWidget QAbstractItemView { selection-background-color: #2563eb; selection-color: #ffffff; } QDialog#datePickerDialog { background: #f8fafc; } QLabel#pickerTitle { color: #172033; font-size: 18px; font-weight: 700; } /* Refined visual layer */ QMainWindow, QWidget#centralWidget { background: #f4f7fc; } QLabel#brandMark { color: #ffffff; background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #2563eb, stop:1 #60a5fa); border-radius: 15px; font-family: Segoe UI; font-size: 22px; font-weight: 700; } QLabel#title { color: #101828; font-size: 29px; font-weight: 700; } QLabel#privacyBadge { color: #344054; background: #ffffff; border: 1px solid #e4eaf2; border-radius: 16px; padding: 7px 13px; font-size: 12px; } QFrame#card, QFrame#resultCard { border-color: #e2e8f0; border-radius: 18px; } QFrame#resultCard[statussuccess] { background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #ffffff, stop:1 #eff6ff); border-color: #bfdbfe; } QLabel#sectionTitle { color: #1d2939; font-size: 17px; } QLabel#cardBadge { border-radius: 10px; padding: 3px 9px; font-size: 11px; font-weight: 700; } QLabel#cardBadge[kindstart] { color: #1d4ed8; background: #eff6ff; } QLabel#cardBadge[kindend] { color: #047857; background: #ecfdf3; } QDateEdit, QTimeEdit { background: #f9fbfd; border-color: #d8e0eb; border-radius: 11px; padding: 11px 13px; } QDateEdit:hover, QTimeEdit:hover { background: #ffffff; border-color: #94a3b8; } QDateEdit:focus, QTimeEdit:focus { padding: 10px 12px; } QDateEdit::drop-down { subcontrol-origin: padding; subcontrol-position: top right; width: 42px; background: #eff6ff; border: 0; border-left: 1px solid #dbeafe; border-top-right-radius: 10px; border-bottom-right-radius: 10px; } QDateEdit::drop-down:hover { background: #dbeafe; } QDateEdit::down-arrow { image: none; width: 0; height: 0; } QPushButton#primaryButton { background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #2563eb, stop:1 #3b82f6); } QLabel#resultHeading { color: #344054; font-size: 14px; } QLabel#statusBadge { border-radius: 10px; padding: 4px 10px; font-size: 11px; font-weight: 700; } QLabel#statusBadge[statusidle] { color: #667085; background: #f2f4f7; } QLabel#statusBadge[statussuccess] { color: #1d4ed8; background: #dbeafe; } QLabel#statusBadge[statuserror] { color: #b42318; background: #fee4e2; } QLabel#calendarResult { color: #172033; font-size: 21px; } QMenuBar { color: #475467; background: #ffffff; border-bottom: 1px solid #e8edf4; padding: 3px 10px; } QMenuBar::item { background: transparent; border-radius: 6px; padding: 6px 12px; } QMenuBar::item:selected { color: #1d4ed8; background: #eff6ff; } QMenu { color: #344054; background: #ffffff; border: 1px solid #dfe6ef; border-radius: 8px; padding: 6px; } QMenu::item { border-radius: 6px; padding: 8px 28px 8px 12px; } QMenu::item:selected { color: #1d4ed8; background: #eff6ff; } QMessageBox { background: #f8fafc; } QMessageBox QLabel { color: #344054; min-width: 270px; } def _add_shadow(widget: QWidget) - None: shadow QGraphicsDropShadowEffect(widget) shadow.setBlurRadius(30) shadow.setOffset(0, 8) shadow.setColor(QColor(20, 35, 60, 24)) widget.setGraphicsEffect(shadow) class DatePickerDialog(QDialog): A calendar dialog with directly clickable year and month selectors. def __init__(self, selected_date: QDate, parent: QWidget | None None) - None: super().__init__(parent) self.setObjectName(datePickerDialog) self.setWindowTitle(选择日期) self.setModal(True) self.setMinimumSize(430, 430) layout QVBoxLayout(self) layout.setContentsMargins(24, 22, 24, 20) layout.setSpacing(16) title QLabel(选择年、月、日) title.setObjectName(pickerTitle) description QLabel(点击年份和月份下拉框再在日历中点击日期) description.setObjectName(subtitle) layout.addWidget(title) layout.addWidget(description) selectors QHBoxLayout() selectors.setSpacing(12) self.year_combo QComboBox() self.year_combo.setObjectName(yearCombo) self.year_combo.addItems(str(year) for year in range(1900, 10000)) self.month_combo QComboBox() self.month_combo.setObjectName(monthCombo) self.month_combo.addItems(f{month} 月 for month in range(1, 13)) selectors.addWidget(QLabel(年份)) selectors.addWidget(self.year_combo, 2) selectors.addWidget(QLabel(月份)) selectors.addWidget(self.month_combo, 1) layout.addLayout(selectors) self.calendar QCalendarWidget() self.calendar.setObjectName(dayCalendar) self.calendar.setNavigationBarVisible(False) self.calendar.setGridVisible(False) self.calendar.setMinimumDate(QDate(1900, 1, 1)) self.calendar.setMaximumDate(QDate(9999, 12, 31)) self.calendar.setSelectedDate(selected_date) self.calendar.setCurrentPage(selected_date.year(), selected_date.month()) layout.addWidget(self.calendar, 1) buttons QDialogButtonBox() self.today_button buttons.addButton(今天, QDialogButtonBox.ButtonRole.ResetRole) self.today_button.setObjectName(secondaryButton) cancel_button buttons.addButton(取消, QDialogButtonBox.ButtonRole.RejectRole) cancel_button.setObjectName(secondaryButton) confirm_button buttons.addButton(确定, QDialogButtonBox.ButtonRole.AcceptRole) confirm_button.setObjectName(primaryButton) layout.addWidget(buttons) self._sync_selectors(selected_date.year(), selected_date.month()) self.year_combo.currentIndexChanged.connect(self._selector_changed) self.month_combo.currentIndexChanged.connect(self._selector_changed) self.calendar.currentPageChanged.connect(self._sync_selectors) self.calendar.activated.connect(lambda _date: self.accept()) self.today_button.clicked.connect(self._select_today) buttons.accepted.connect(self.accept) buttons.rejected.connect(self.reject) def _sync_selectors(self, year: int, month: int) - None: self.year_combo.blockSignals(True) self.month_combo.blockSignals(True) self.year_combo.setCurrentIndex(year - 1900) self.month_combo.setCurrentIndex(month - 1) self.year_combo.blockSignals(False) self.month_combo.blockSignals(False) def _selector_changed(self) - None: year self.year_combo.currentIndex() 1900 month self.month_combo.currentIndex() 1 current self.calendar.selectedDate() day min(current.day(), QDate(year, month, 1).daysInMonth()) selected QDate(year, month, day) self.calendar.setCurrentPage(year, month) self.calendar.setSelectedDate(selected)4 源码和软件下载软件下载地址时间差计算工具计算两个日期、时刻之间的时间差https://mbd.pub/o/bread/YZaVlJxvaw