JavaScript fails to handle Daylight Saving Time Problem (Known Issue)
Article Metadata
Code Example
Compatibility
Article
Contents |
Overview
JavaScript's Date() function does not take care of Daylight Time Saving, resulting erroneous result in those regions where DST is maintained. So, A specific date picked from QML's DatePickerDialog might give wrong value while fed to JScript Date. QDateTime can be used for both date and time which is according to DST. Here this takes care of Daylight Saving Time from the system, unlike JavaScript and shows proper value.
In the following code snippet, 'date' is a string which gets value from Qt's QDateTime, so, even if the time zone is set where DST is maintained, it always gets proper date value. 'dt' is an object of JavaScript Date, which gets value from DatePickerDialog. This will show a wrong value where DST is maintained.
Steps to reproduce the problem
- Set the timezone of the device as Brasilia, Brazil(GMT+3).
- Pick a date from date picker dialog as 20th March 2012.
- JScript's Date() will display 19th March 2012, whereas QDateTime will fetch 20th March 2012.
Work Around
Using Qt's QDateTime object will resolve this problem of DST. The following snippet shows how to use both JavaScript's Date() and QDateTime object along with QML's DatePickerDialog.
QML File
import QtQuick 1.0
import com.nokia.extras 1.0
Rectangle {
width: 360
height: 720
property string dateStr;
DatePickerDialog {
id: tDialog
titleText: "Date"
acceptButtonText: qsTr("Ok")
onAccepted: {
datePickerAccepted();
}
}
function datePickerAccepted(){
// 'date' could be used to display actual date picked from dialog
dateStr= tMainView.getDate(tDialog.year,tDialog.month,tDialog.day);
// Following code snippet does not take DST into account.
// The snippet will give wrong Date/Time value where DST is maintained
// For DST, set “dateStr” date and time values to JS Date in the below code.
var dt=new Date();
dt.setFullYear(tDialog.year);
dt.setMonth(tDialog.month-1);//dateDialog default starts on 1. Javascript starts on 0
dt.setDate(tDialog.day);//dateDialog default starts on 1
jsdt.text= "JS Date: "+Qt.formatDateTime(dt, Qt.DefaultLocaleShortDate );
}
MouseArea {
anchors.fill: parent
onClicked: {
tDialog.open();
}
}
}
Header File
#include <QMainWindow>
#include <QDateTime>
#include <QWidget>
#include <QDeclarativeView>
class tMainView : public QMainWindow
{
Q_OBJECT
public:
explicit tMainView(QWidget *parent = 0);
Q_INVOKABLE QString getDate(int y,int m,int d);
private:
QDeclarativeView * DecView;
};
Source File
tMainView::tMainView(QWidget *parent) :
QMainWindow(parent)
{
QWidget* pWidget = new QWidget();
setCentralWidget(pWidget);
DecView = new QDeclarativeView(pWidget);
DecView->rootContext()->setContextProperty("tMainView", this );
DecView->setSource(QUrl("qrc:/qml/DatePickerQtEx/main.qml"));
}
QString tMainView::getDate(int y, int m,int d)
{
QDateTime now = QDateTime::currentDateTime();
QTime nowTime=now.time();
QDate date(y,m,d);
QString str("Date: "+date.toString()+" "+nowTime.toString());
QMessageBox dateBox;
dateBox.setText(str);
dateBox.exec();
return str;
}

