How to det Date from String. QDate and Qtime?
-
I have Date and Time values as String. I want to synchronize these values with QTime and Qdate and want the time to flow from the values I gave instead of Current time, how can I do this?
Here is mu finction
void MainWindow::on_syncdatebtn_clicked() { QStringList dtstr = dtcal.split("."); dtcal.remove("."); serial.send(QString::fromUtf8("WR+1160")+dtcal); serial.waitdata(); QString getdtcal=serial.getString(); serial.waitdata(); QStringList getdtcalbuf = getdtcal.split(","); qDebug()<< getdtcalbuf; QString Timeformat = getdtcalbuf[2]+"."+getdtcalbuf[3]+"."+getdtcalbuf[4]; QString Dateformat = getdtcalbuf[5]+"."+getdtcalbuf[6]+"."+getdtcalbuf[7]; qDebug()<<Timeformat << " " << Dateformat; qDebug()<< dtcal; QTime Time =QTime::fromString(Timeformat,"hh.mm.ss"); QDate Date = QDate::fromString(Dateformat,"dd.MM.yy"); qDebug()<< Time << " "<<Date; //QTime(Invalid) QDate(Invalid) }
-
TimeFormat "21.25.15" DateFormat "2.10.22"
So is there any other way I can set it to custom instead of the system clock?
@TheCeylann said in How to det Date from String. QDate and Qtime?:
DateFormat "2.10.22"
This does not match the format "dd.MM.yy", also you should add 2000 to your year - QDateTime assumes 1900 if only two digits are given for the yeas.
The time is correctly converted:
QString Timeformat("21.25.15"); QString Dateformat("02.10.22"); QTime Time = QTime::fromString(Timeformat, "hh.mm.ss"); QDate Date = QDate::fromString(Dateformat, "dd.MM.yy"); qDebug() << Time << " " << Date; --> QTime("21:25:15.000") QDate("1922-10-02")
-
Hi,
What is the content of your variables ?
-
@SGaist said in How to det Date from String. QDate and Qtime?:
Değişkenlerinizin içeriği nedir?
Hello
getdtcalbuf returns me("CMD", "OK", "21", "12", "57", "2", "10", "22", "DHC", "\r\n")
This data comes to me from the serial port, I use the first two for control, the rest are in order;
getdtcalbuf[2] = hour getdtcalbuf[3] = minute getdtcalbuf[4] = second getdtcalbuf[5] = day getdtcalbuf[6] = month getdtcalbuf[7] = year
-
And what is the content of your TimeFormat and DateFormat variables ?
On a side note, these variable names are misleading. They are the date and time values not their format.
-
And what is the content of your TimeFormat and DateFormat variables ?
On a side note, these variable names are misleading. They are the date and time values not their format.
TimeFormat "21.25.15" DateFormat "2.10.22"
So is there any other way I can set it to custom instead of the system clock?
-
The invalid date comes from a mismatch between your input string and the format.
Are you sure that your time input does not suffer the same issue ?
-
The invalid date comes from a mismatch between your input string and the format.
Are you sure that your time input does not suffer the same issue ?
@SGaist It doesn't happen when I set the time via String. But when I do it with current DateTime() it works fine.
i'm Sorry I forgot myself. -
Sorry, Turkish is not one of the language I speak.
-
TimeFormat "21.25.15" DateFormat "2.10.22"
So is there any other way I can set it to custom instead of the system clock?
@TheCeylann said in How to det Date from String. QDate and Qtime?:
DateFormat "2.10.22"
This does not match the format "dd.MM.yy", also you should add 2000 to your year - QDateTime assumes 1900 if only two digits are given for the yeas.
The time is correctly converted:
QString Timeformat("21.25.15"); QString Dateformat("02.10.22"); QTime Time = QTime::fromString(Timeformat, "hh.mm.ss"); QDate Date = QDate::fromString(Dateformat, "dd.MM.yy"); qDebug() << Time << " " << Date; --> QTime("21:25:15.000") QDate("1922-10-02")
-
@TheCeylann said in How to det Date from String. QDate and Qtime?:
DateFormat "2.10.22"
This does not match the format "dd.MM.yy", also you should add 2000 to your year - QDateTime assumes 1900 if only two digits are given for the yeas.
The time is correctly converted:
QString Timeformat("21.25.15"); QString Dateformat("02.10.22"); QTime Time = QTime::fromString(Timeformat, "hh.mm.ss"); QDate Date = QDate::fromString(Dateformat, "dd.MM.yy"); qDebug() << Time << " " << Date; --> QTime("21:25:15.000") QDate("1922-10-02")
@Christian-Ehrlicher Thank you. It works