×
Namespaces

Variants
Actions

Detecting day of week in QML

From Nokia Developer Wiki
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
Symbian
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
This page was last modified on 11 October 2012, at 04:16.
59 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.

×