Hour

Returns the current hour.

Syntax

Hour(): number

Return Value

Returns a number representing the current hour (0-23).

Description

The Hour method returns the hour component of the current time. The returned value is between 0 and 23, representing the hour in 24-hour format.

Example

// Get current hour
const hour = this.api.Hour()
console.log(`Current hour: ${hour}`)

// Format hour with leading zero
const formattedHour = hour.toString().padStart(2, '0')
console.log(`Formatted hour: ${formattedHour}`)

// Check for trading hours (example: 9:30 - 16:00)
const isMarketHour = hour >= 9 && hour <= 16
console.log(`Within market hours: ${isMarketHour}`)

// Convert to 12-hour format
const hour12 = hour % 12 || 12
const amPm = hour < 12 ? 'AM' : 'PM'
console.log(`Time: ${hour12}${amPm}`)

// Check for specific periods
if (hour < 12) {
    console.log('Morning session')
} else if (hour < 17) {
    console.log('Afternoon session')
} else {
    console.log('Evening session')
}

Last updated