Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

JavaScript fails to handle Daylight Saving Time Problem (Known Issue)

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleCompatibility
Platform(s): Symbian^1 and later
Article
Created: debjit.roy (17 Apr 2012)
Last edited: hamishwillee (11 Oct 2012)

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

  1. Set the timezone of the device as Brasilia, Brazil(GMT+3).
  2. Pick a date from date picker dialog as 20th March 2012.
  3. 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;
}

See Also

http://www.developer.nokia.com/Community/Wiki/DatePicker_and_TimePicker_Dialogs_in_MeeGo_Qt_Quick_Extras_Components

https://bugreports.qt-project.org/browse/QTBUG-25624

This page was last modified on 11 October 2012, at 01:17.
63 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×