Qt:日期与时间戳互相转换Demo

1、dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QRadioButton>
#include <QPushButton>

class Dialog : public QDialog
{
    Q_OBJECT    
public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
    
private:
    enum ESinceEpochType {
        EN_Secs = 0,
        EN_MSecs,
    };
    
    void init();
    void initLayout();
    
private slots:
    void onConvertToSinceEpoch();
    void onConvertToDtime();
    
private:
    QLabel          *m_lblYear = nullptr;
    QLineEdit       *m_edtYear = nullptr;
    QLabel          *m_lblMonth = nullptr;
    QLineEdit       *m_edtMonth = nullptr;
    QLabel          *m_lblDay = nullptr;
    QLineEdit       *m_edtDay = nullptr;

    QLabel          *m_lblHour = nullptr;
    QLineEdit       *m_edtHour = nullptr;
    QLabel          *m_lblMinute = nullptr;
    QLineEdit       *m_edtMinute = nullptr;
    QLabel          *m_lblSecond = nullptr;
    QLineEdit       *m_edtSecond = nullptr;
    
    ESinceEpochType m_eSinceEpochType = EN_Secs;
    QRadioButton    *m_rbtnSecsSinceEpoch = nullptr;
    QRadioButton    *m_rbtnMSecsSinceEpoch = nullptr;
    
    QPushButton     *m_btnConvert = nullptr;
    QLabel          *m_lblTimestamp = nullptr;
    QLineEdit       *m_edtTimestamp = nullptr;
    
    QPushButton     *m_btnConvert2 = nullptr;
    QLabel          *m_lblDateTime = nullptr;
    QLineEdit       *m_edtDateTime = nullptr;
};

#endif // DIALOG_H

2、dialog.cpp

#include "dialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDateTime>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    init();
    initLayout();
}

Dialog::~Dialog()
{
}

void Dialog::init()
{
    m_lblYear = new QLabel("年", this);
    m_edtYear = new QLineEdit("2022", this);
    m_lblMonth = new QLabel("月", this);
    m_edtMonth = new QLineEdit("12", this);
    m_lblDay = new QLabel("日", this);
    m_edtDay = new QLineEdit("28", this);
    
    m_lblHour = new QLabel("时", this);
    m_edtHour = new QLineEdit("14", this);
    m_lblMinute = new QLabel("分", this);
    m_edtMinute = new QLineEdit("43", this);
    m_lblSecond = new QLabel("秒", this);
    m_edtSecond = new QLineEdit("30", this);
    
    m_rbtnSecsSinceEpoch = new QRadioButton("时间戳(秒)", this);
    m_rbtnMSecsSinceEpoch = new QRadioButton("时间戳(毫秒)", this);
    
    m_btnConvert = new QPushButton("转换为时间戳", this);
    m_lblTimestamp = new QLabel("时间戳:", this);
    m_edtTimestamp = new QLineEdit(this);
    
    m_btnConvert2 = new QPushButton("转换为日期", this);
    m_lblDateTime = new QLabel("日期:", this);
    m_edtDateTime = new QLineEdit(this);
    
    m_edtYear->setFixedSize(100, 30);
    m_edtMonth->setFixedSize(100, 30);
    m_edtDay->setFixedSize(100, 30);
    m_edtHour->setFixedSize(100, 30);
    m_edtMinute->setFixedSize(100, 30);
    m_edtSecond->setFixedSize(100, 30);
    
    m_btnConvert->setFixedSize(100, 30);
    m_edtTimestamp->setFixedSize(200, 30);
    m_btnConvert2->setFixedSize(100, 30);
    m_edtDateTime->setFixedSize(200, 30);
    m_edtDateTime->setEnabled(false);
    
    m_rbtnSecsSinceEpoch->setChecked(true);
    m_rbtnMSecsSinceEpoch->setChecked(false);
    m_eSinceEpochType = EN_Secs;
    
    connect(m_rbtnSecsSinceEpoch, &QRadioButton::toggled, this, [this] {
        m_eSinceEpochType = EN_Secs;
        onConvertToSinceEpoch();
        onConvertToDtime();
    });
    
    connect(m_rbtnMSecsSinceEpoch, &QRadioButton::toggled, this, [this] {
        m_eSinceEpochType = EN_MSecs;
        onConvertToSinceEpoch();
        onConvertToDtime();
    });
    
    connect(m_btnConvert, &QPushButton::clicked, this, &Dialog::onConvertToSinceEpoch);
    
    connect(m_btnConvert2, &QPushButton::clicked, this, &Dialog::onConvertToDtime);
}

void Dialog::initLayout()
{
    QHBoxLayout *hLay1 = new QHBoxLayout();
    hLay1->setMargin(0);
    hLay1->setSpacing(16);
    hLay1->addWidget(m_edtYear);
    hLay1->addWidget(m_lblYear);
    hLay1->addWidget(m_edtMonth);
    hLay1->addWidget(m_lblMonth);
    hLay1->addWidget(m_edtDay);
    hLay1->addWidget(m_lblDay);
    
    QHBoxLayout *hLay2 = new QHBoxLayout();
    hLay2->setMargin(0);
    hLay2->setSpacing(16);
    hLay2->addWidget(m_edtHour);
    hLay2->addWidget(m_lblHour);
    hLay2->addWidget(m_edtMinute);
    hLay2->addWidget(m_lblMinute);
    hLay2->addWidget(m_edtSecond);
    hLay2->addWidget(m_lblSecond);
    
    QHBoxLayout *hLayRbtn = new QHBoxLayout();
    hLayRbtn->setMargin(0);
    hLayRbtn->setSpacing(20);
    hLayRbtn->addStretch(0);
    hLayRbtn->addWidget(m_rbtnSecsSinceEpoch);
    hLayRbtn->addWidget(m_rbtnMSecsSinceEpoch);
    hLayRbtn->addStretch(0);
    
    QHBoxLayout *hLay3 = new QHBoxLayout();
    hLay3->setMargin(0);
    hLay3->setSpacing(16);
    hLay3->addWidget(m_btnConvert);
    hLay3->addStretch();
    hLay3->addWidget(m_lblTimestamp);
    hLay3->addWidget(m_edtTimestamp);
    
    QHBoxLayout *hLay4 = new QHBoxLayout();
    hLay4->setMargin(0);
    hLay4->setSpacing(16);
    hLay4->addWidget(m_btnConvert2);
    hLay4->addStretch();
    hLay4->addWidget(m_lblDateTime);
    hLay4->addWidget(m_edtDateTime);
    
    QVBoxLayout *vLayMain = new QVBoxLayout(this);
    vLayMain->setMargin(60);
    vLayMain->setSpacing(20);
    vLayMain->addLayout(hLay1);
    vLayMain->addLayout(hLay2);
    vLayMain->addLayout(hLayRbtn);
    vLayMain->addLayout(hLay3);
    vLayMain->addLayout(hLay4);
    this->setLayout(vLayMain);
}

void Dialog::onConvertToSinceEpoch()
{
    QString strDtime = QString("%1-%2-%3 %4:%5:%6")
            .arg(m_edtYear->text(), m_edtMonth->text(), m_edtDay->text(), 
                 m_edtHour->text(), m_edtMinute->text(), m_edtSecond->text());
    QDateTime dt = QDateTime::fromString(strDtime, "yyyy-MM-dd hh:mm:ss");
    
    if (dt.isValid()) {
        qint64 timeStamp;
        if (EN_Secs == m_eSinceEpochType)
            timeStamp = dt.toSecsSinceEpoch();
        else if (EN_MSecs == m_eSinceEpochType)
            timeStamp = dt.toMSecsSinceEpoch();
        else
            timeStamp = 0;
        m_edtTimestamp->setText(QString::number(timeStamp));
    }
}

void Dialog::onConvertToDtime()
{
    QDateTime dt;
    if (EN_Secs == m_eSinceEpochType)
        dt = QDateTime::fromSecsSinceEpoch(m_edtTimestamp->text().toLongLong());
    else if (EN_MSecs == m_eSinceEpochType)
        dt = QDateTime::fromMSecsSinceEpoch(m_edtTimestamp->text().toLongLong());
    
    if (dt.isValid())
        m_edtDateTime->setText(dt.toString("yyyy-MM-dd hh:mm:ss"));
}

3、运行界面:

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宏笋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值