Revision as of 04:17, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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



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: {
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.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: {

Header File

#include <QMainWindow>
#include <QDateTime>
#include <QWidget>
#include <QDeclarativeView>
class tMainView : public QMainWindow
explicit tMainView(QWidget *parent = 0);
Q_INVOKABLE QString getDate(int y,int m,int d);
QDeclarativeView * DecView;

Source File

tMainView::tMainView(QWidget *parent) :
QWidget* pWidget = new QWidget();
DecView = new QDeclarativeView(pWidget);
DecView->rootContext()->setContextProperty("tMainView", this );
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;
return str;

See Also



This page was last modified on 11 October 2012, at 04:17.
100 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.