返回当前日期和时间设置

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. BugHD 与你的应用一起成长

    BugHD 新增功能 1.新增安装量.启动量的统计功能 BugHD SDK 1.3.0新增安装量.启动量的统计功能,在崩溃分析页面右上角可看到安装量和启动量. BugHD 体验优化 1.Android ...

  2. 在MVC中实现和网站不同服务器的批量文件下载以及NPOI下载数据到Excel的简单学习

    嘿嘿,我来啦,最近忙啦几天,使用MVC把应该实现的一些功能实现了,说起来做项目,实属感觉蛮好的,即可以学习新的东西,又可以增加自己之前知道的知识的巩固,不得不说是双丰收啊,其实这周来就开始面对下载在挣 ...

  3. Git和GitHub入门基础

    -----------------------------------------//cd F:/learngit // 创建仓库git init  // 在当前目录下创建空的git仓库------- ...

  4. Java网络编程之查找Internet地址

    一.概述 连接到Internet上计算机都有一个称为Internet地址或IP地址的唯一的数来标识.由于IP很难记住,人们设计了域名系统(DNS),DNS可以将人们可以记忆的主机名与计算机可以记忆的I ...

  5. css笔记 - 张鑫旭css课程笔记之 relative 篇

    relative地址 relative 对 absolute的限制作用 限制left/top/right/bottom(方位值)定位 限制描述:absolute设置了方位值时,这些方位值是相对于pos ...

  6. MFC onchar()

    为什么在CView类中可以对ON_CHAR进行相应,添加消息处理函数onchar就可以了,但是在CDialog中要对ON_CHAR相应,直接添加不行? CView相当于Text控件,你可以在Text控 ...

  7. Linux 下安装JDK1.8

    本文主要介绍的是如何是Linux环境下安装JDK的,因为Linux环境下,很多时候也离不开Java的,下面笔者就和大家一起分享如何jdk1.8的过程吧. 一.安装环境 操作系统:Red Hat Ent ...

  8. 简单了解如何使用vue-router和vue-resource

    我们先来看看vue-router 1.npm install vue-router --save 2.调用vue-router: 第一种方法: 直接在main.js中调用 import vueRout ...

  9. [转]redhat7(centos7) not registered to Red Hat Subscription Management

    [root@controller0 ~]# yum install ntp Loaded plugins: fastestmirror, product-id, search-disabled-rep ...

  10. WIN7开启wifi热点

    1.首先,先确定自己的笔记本网卡支持“启动承载网络”的功能,使用管理员运行cmd命令.启用管理员运行CMD的方法Windows-所有程序-附件-运行(右键,以管理员身份运行)在弹出的CMD窗口里面敲击 ...