|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications | ![]() |
[Prev: Boolean] [Home] [Next: Function Type]
Instances of the Date class are used to store and manipulate dates and times.
A variety of get functions are provided to obtain the date, time or relevant parts, for example, getDay(), getYear(), getHours(), getMilliseconds(), getMinutes(), getMonth(), getSeconds(), getTime().
A complementary set of functions are also provided, including setYear(), setHours(), setMilliseconds(), setMinutes(), setMonth(), setSeconds(), setTime().
The functions operate using local time.
Conversion between Date objects to and from strings are provided by parse() and Date::toString().
Elapsed time (in milliseconds) can be obtained by creating two dates, casting them to Number and subtracting one value from the other.
var date1 = new Date(); // time flies.. var date2 = new Date(); var timedifference = date2.getTime() - date1.getTime();
parse( dateString : String ) : Number;
var d = new Date( Date.parse( "1976-01-25T22:30:00" ) ); d = Date.parse( "1976-01-25T22:30:00" );
This is a static function that parses a string, dateString, which represents a particular date and time. It returns the number of milliseconds since midnight on the 1st January 1970. The string must be in the ISO 8601 extended format: YYYY-MM-DD or with time YYYY-MM-DDTHH:MM:SS.
Date() Date( milliseconds ) Date( year, month, day, optHour, optMinutes, optSeconds, optMilliseconds )
var today = new Date(); var d = new Date( 1234567 ); var date = new Date( 1994, 4, 21 ); var moment = new Date( 1968, 5, 11, 23, 55, 30 );
Dates can be constructed with no arguments, in which case the value is the date and time at the moment of construction using local time. A single integer argument is taken as the number of milliseconds since midnight on the 1st January 1970.
getDate() : Number;
var d = new Date( 1975, 12, 25 ); var x = d.getDate(); // x == 25
Returns the day of the month using local time. The value is always in the range 1..31.
getDay() : Number;
var d = new Date( 1975, 12, 25, 22, 30, 15 ); var x = d.getDay(); // x == 4
Returns the day of the week using local time. The value is always in the range 1..7, with the week considered to begin on Monday.
Example:
var IndexToDay = [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" ]; var d = new Date( 1975, 12, 28 ); System.println( IndexToDay[ d.getDay() - 1 ] ); // Prints "Sun"
getYear() : Number;
var d = new Date( 1975, 12, 25 ); var x = d.getYear(); // x == 1975
Returns the year using local time.
getHours() : Number;
var d = new Date( 1975, 12, 25, 22 ); var x = d.getHours(); // x == 22
Returns the hour using local time. The value is always in the range 0..23.
getMilliseconds() : Number;
var d = new Date( 1975, 12, 25, 22 ); var x = d.getMilliseconds(); // x == 0
Returns the milliseconds component of the date using local time. The value is always in the range 0..999. In the example, x is 0, because no milliseconds were specified, and the default for unspecified components of the time is 0.
getMinutes() : Number;
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getMinutes(); // x == 30
Returns the minutes component of the date using local time. The value is always in the range 0..59.
getMonth() : Number;
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getMonth(); // x == 12
Returns the months component of the date using local time. The value is always in the range 1..12.
Example:
var IndexToMonth = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; var d = new Date( 1975, 12, 25 ); System.println( IndexToMonth[ d.getMonth() - 1] ); // Prints "Dec"
getSeconds() : Number;
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getSeconds(); // x == 0
Returns the seconds component of the date using local time. The value is always in the range 0..59. In the example x is 0 because no seconds were specified, and the default for unspecified components of the time is 0.
getTime() : Number;
var d = new Date( 1975, 12, 25, 22, 30 ); var x = d.getTime(); // x == 1.91457e+11
Returns the number of milliseconds since midnight on the 1st January 1970 using local time.
setDate( dayOfTheMonth : Number );
var d = new Date( 1975, 12, 25, 22, 30 ); d.setDate( 30 ); // d == 1975-12-30T22:30:00
Sets the day of the month to the specified dayOfTheMonth in local time.
setYear( year : Number );
var d = new Date( 1975, 12, 25, 22, 30 ); d.setYear( 1980 ); // d == 1980-12-30T22:30:00
Sets the year to the specified year in local time.
setHours( hour : Number );
var d = new Date( 1975, 12, 25, 22, 30 ); d.setHours( 10 ); // d == 1980-12-30T10:30:00
Sets the hour to the specified hour, which must be in the range 0..23, in local time. The minutes, seconds and milliseconds past the hour (optMinutes, optSeconds and optMilliseconds) can also be specified.
setMilliseconds( milliseconds : Number );
var d = new Date( 1975, 12, 25, 22, 30 ); d.setMilliseconds( 998 ); // d == 1980-12-30T10:30:00:998
Sets the milliseconds component of the date to the specified value in local time.
setMinutes( minutes : Number );
var d = new Date( 1975, 12, 25, 22, 30 ); d.setMinutes( 15 ); // d == 1980-12-30T10:15:00
Sets the minutes to the specified minutes, which must be in the range 0..59, in local time. The seconds and milliseconds past the minute (optSeconds and optMilliseconds) can also be specified.
setMonth( month : Number );
var d = new Date( 1975, 12, 25, 22, 30 ); d.setMonth( 0 ); // d == 1980-01-11T22:30:00
Sets the month to the specified month, which must be in the range 0..11, in local time.
setSeconds()
setSeconds( seconds )
var d = new Date( 1975, 12, 25, 22, 30 ); d.setSeconds( 25 ); // d == 1980-12-30T22:30:25
Sets the seconds to the specified seconds, which must be in the range 0..59, in local time.
setTime( milliseconds : Number );
var d = new Date( 1975, 12, 25, 22, 30 ); var duplicate = new Date(); duplicate.setTime( d.getTime() );
Sets the date and time to the local date and time given in terms of milliseconds since midnight on the 1st January 1970.
toString() : String;
var d = new Date( 1975, 12, 25, 22, 30 ); var s = d.toString(); // s == "1975-12-25T22:30:00"
Converts the date into a string on the ISO 8601 extended format: YYYY-MM-DDTHH:MM:SS.
[Prev: Boolean] [Home] [Next: Function Type]
Copyright © 2001-2006 Trolltech | Trademarks | QSA version 1.1.5
|