返回当前日期和时间设置

from PyQt5 import QtCore, QtWidgets

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.setWindowModality(QtCore.Qt.WindowModal)
Dialog.resize(690, 437)
Dialog.setAutoFillBackground(True)
Dialog.setSizeGripEnabled(True)
self.calendarWidget = QtWidgets.QCalendarWidget(Dialog)
self.calendarWidget.setGeometry(QtCore.QRect(230, 30, 431, 291))
self.calendarWidget.setObjectName("calendarWidget")
self.dateEdit = QtWidgets.QDateEdit(Dialog)
self.dateEdit.setGeometry(QtCore.QRect(30, 80, 171, 22))
self.dateEdit.setObjectName("dateEdit")
self.dateTimeEdit = QtWidgets.QDateTimeEdit(Dialog)
self.dateTimeEdit.setEnabled(True)
self.dateTimeEdit.setGeometry(QtCore.QRect(10, 190, 211, 31))
self.dateTimeEdit.setToolTip("")
self.dateTimeEdit.setWrapping(False)
self.dateTimeEdit.setFrame(True) #边框可见
self.dateTimeEdit.setReadOnly(False) #设置只读模式
self.dateTimeEdit.setButtonSymbols(QtWidgets.QAbstractSpinBox.UpDownArrows)
self.dateTimeEdit.setSpecialValueText("")
self.dateTimeEdit.setKeyboardTracking(True)
self.dateTimeEdit.setCurrentSection(QtWidgets.QDateTimeEdit.DaySection)
self.dateTimeEdit.setCalendarPopup(True)
self.dateTimeEdit.setCurrentSectionIndex(3)
self.dateTimeEdit.setTimeSpec(QtCore.Qt.LocalTime)
self.dateTimeEdit.setObjectName("dateTimeEdit") self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog")) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

UI文件

main文件中设置(将获取当前日期时间的代码放在main文件中,避免QT设计师更改界面时 代码改动)

# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets
from Ui_QDataTimeEdit import Ui_Dialog class Dialog(QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.dateTimeEdit.setDateTime(QDateTime.currentDateTime())
self.dateEdit.setDate(QDate.currentDate())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Dialog()
ui.show()
sys.exit(app.exec_())

一些常用的技巧:

self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.widget)
self.dateTimeEdit.setFrame(False) #去除边框
self.dateTimeEdit.setDate(QtCore.QDate(2018, 10, 25)) #设置日期
self.dateTimeEdit.setTime(QtCore.QTime(11, 0, 0)) #设置时间11:00
self.dateTimeEdit.setObjectName("dateTimeEdit") #设置对象名称 """
方式1:
from PyQt5.QtCore import QDate,QTime
##设置为当前日期
self.dateTimeEdit.setDateTime(QDate.currentDate())
##设置为当前时间
self.dateTimeEdit.setDateTime(QTime.currentTime()) 方式2:
from PyQt5.QtCore import QDateTime
##设置当前日期和时间
self.dateTimeEdit.setDateTime(QDateTime.currentDateTime())
"""
##同理设置dataEdit是一样的
from PyQt5.QtCore import QDate
self.dateEdit.setDate(QDate.currentDate())

打印出不同格式的日期和时间

from PyQt5.QtCore import QDate, QTime, QDateTime, Qt

"""返回当前日期"""
now = QDate.currentDate()
print(now) #PyQt5.QtCore.QDate(2018, 12, 3)
print(now.toString()) #周一 12月 3 2018
print(now.toString(Qt.ISODate)) #2018-12-03
print(now.toString(Qt.DefaultLocaleLongDate)) #2018年12月3日, 星期一 """返回当前日期和时间"""
datetime = QDateTime.currentDateTime()
print(datetime) #PyQt5.QtCore.QDateTime(2018, 12, 3, 15, 9, 41, 976)
print(datetime.toString()) #周一 12月 3 15:09:41 2018
print(datetime.toString(Qt.ISODate)) #2018-12-03T15:09:41
print(datetime.toString(Qt.DefaultLocaleLongDate)) #2018年12月3日, 星期一 15:09:41 """返回当前时间"""
time = QTime.currentTime()
print(time) #PyQt5.QtCore.QTime(15, 12, 9, 980)
print(time.toString()) #15:12:09
print(time.toString(Qt.ISODate)) #15:12:09
print(time.toString(Qt.DefaultLocaleLongDate)) #15:12:09

【pyqt5】QdateTimeEdit(日期时间)的更多相关文章

  1. PyQt(Python+Qt)学习随笔:QDateTimeEdit日期时间编辑部件

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 Designer输入部件中,Date/Time E ...

  2. EasyUI datagrid 日期时间格式化

    EasyUI datagrid中显示日期时间时,会显示为以下不太直观的数值: 添加以下JavaScript脚本,然后在field中添加 formatter: DateTimeFormatter 即可. ...

  3. POCO库——Foundation组件之日期时间DateTime

    日期时间DateTime:内部提供多个设计计时器.日期.时区.时间戳等: Clock.h :Clock时钟计时类,_clock:Int64类型时钟值,CLOCKVAL_MIN.CLOCKVAL_MAX ...

  4. db2 日期时间格式

    db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...

  5. Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例

    Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...

  6. MySQL学习笔记八:日期/时间的处理

    MySQL日期时间的处理,在其官网文档上都有详细的阐述,想了解更多的同学可自行查阅. 1.查询当前日期时间:函数有now(),localtime(),current_timestamp(),sysda ...

  7. Java日期时间操作的一些方法

    1. 获得Calendar实例: Calendar c = Calendar.getInstance(); 2. 定义日期/时间的格式: SimpleDateFormat sdf =new Simpl ...

  8. mysql与oracle的日期/时间函数小结

    前言 本文的日期/时间全部格式化为”2016-01-01 01:01:01“形式: MONITOR_TIME为数据库表字段: 字符串与日期/时间相互转换函数 Oracle 日期/时间转字符串函数:to ...

  9. js 日期时间排序 数组

    不多说直接show代码 var timeArr=[ {'id':'A01','date':'2016-04-20 23:22:11'}, {'id':'A02','date':'2016-04-21 ...

  10. sql server日期时间转字符串

    一.sql server日期时间函数Sql Server中的日期与时间函数 1.  当前系统日期.时间     select getdate()  2. dateadd  在向指定日期加上一段时间的基 ...

随机推荐

  1. OBS显示器获取显示黑色没有图像

  2. JavaWeb学习总结(十六)Cookie保存中文内容

    Cookie的值保存中文内容,可以使用Java.net.URLDecoder进行解码. 示例: <%@page import="java.net.URLDecoder"%&g ...

  3. Android分包原理

    如果App引用的库太多,方法数超过65536后无法编译.这是因为单个dex里面不能有超过65536个方法.为什么有最大的限制呢,因为Android会把每一个类的方法id检索起来,存在一个链表结构里面. ...

  4. 【Java并发编程三】闭锁

    1.什么是闭锁? 闭锁(latch)是一种Synchronizer(Synchronizer:是一个对象,它根据本身的状态调节线程的控制流.常见类型的Synchronizer包括信号量.关卡和闭锁). ...

  5. 使用API函数EnumWindows()枚举顶层窗口

      http://simpleease.blog.163.com/blog/static/1596085820052770290/ 要枚举Windows当前所有打开的顶层窗口,可使用Windows A ...

  6. Nginx/LVS/HAProxy负载均衡软件的优缺点

    一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术.具体的应用需求还得具体分析,如果是中小型的Web应用,比如日PV小于1000万,用Nginx就完全可以了:如果机器不少,可以用D ...

  7. KMP算法的实现(Java语言描述)

    标签:it KMP算法是模式匹配专用算法. 它是在已知模式串的next或nextval数组的基础上执行的.如果不知道它们二者之一,就没法使用KMP算法,因此我们需要计算它们. KMP算法由两部分组成: ...

  8. [原]linux下将网卡设置为混杂模式

    设置为混杂模式ifconfig eth2 promisc取消设置ifconfig eth2 -promisc ------------------------------------------ 下面 ...

  9. [转]mii-tool与ethtool的用法详解

    1.mii-tool 配置网络设备协商方式的工具: 感谢原文作者!原文地址:http://blog.chinaunix.net/uid-20639775-id-154546.html 1.1 mii- ...

  10. 使用eclipse执行maven-release-plugin插件发布jar异常问题(.project)(Cannot prepare the release because you have local modifications )

    开发是用的eclipse,里面有工程文件.project这种文件,运行release:prepare的时候报异常: Cannot prepare the release because you hav ...