Namespaces
Variants
Actions
Revision as of 04:16, 11 October 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Detecting day of week in QML

Jump to: navigation, search

This article explains how to detect day of week in QML, how to detect localized name, how to link localized name with Qt enum.The code example utilizes the formatDateTime method.

Article Metadata

Tested with
SDK: Nokia Qt SDK 1.2
Devices(s): all devices, based on Symbian Anna, Nokia Belle

Compatibility
Platform(s): Symbian Anna, Nokia Belle
Device(s): All
Dependencies: None

Platform Security
Capabilities: None

Article
Keywords: QML, Qt, Day of week
Created: Den123 (11 Mar 2012)
Last edited: hamishwillee (11 Oct 2012)

QML global Qt object provides enum for defining days of week:

Qt.Monday  = 1
Qt.Tuesday = 2
...
Qt.Sunday = 7

Unfortunately, Date object does not contain functionality for using this enum. But we can use Qt.formatDateTime() function for detecting localized name of day:

var myDate = new Date() // current date
var localizedDayName = Qt.formatDateTime( myDate, "dddd" ) // current day name

We can use a little trick for linking localized day name with Qt enum:

function localizedDayNameByIdx( idx )
{
if( idx >= Qt.Monday && idx <= Qt.Sunday )
{
var myDate = new Date()
 
myDate.setFullYear( 1980, 2, 17 ) // March 17, 1980 - we know, it was monday
myDate.setDate( myDate.getDate() + idx - 1 ) // Qt.Monday = 1
 
return Qt.formatDateTime( myDate, "dddd" )
}
 
return ""
}
 
function dayIdxByLocalizedDayName( dayName )
{
var res = -1
 
var myDate = new Date()
myDate.setFullYear( 1980, 2, 17 ) // March 17, 1980 - we know, it was monday
for( var i = 0; i < 7; i ++)
if( Qt.formatDateTime( myDate, "dddd" ) == dayName )
{
res = i + 1;
break
}
else
myDate.setDate( myDate.getDate() + 1 )
 
return res
}

Example of use for the Russian localization:

- localizedDayNameByIdx( Qt.Monday ) // returns "понедельник"
- dayIdxByLocalizedDayName( "понедельник" ) // returns Qt.Monday
313 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved