Time
Returns the time for a specific bar.
Syntax
Time(shift: number, timeZoneMode?: TimeZoneMode): FTODate
Parameters
shift
: A number representing the shift from the current bartimeZoneMode
: Optional. Default value is project timezone. ATimeZoneMode
enum value representing the timezone mode to use for the returned date
Return Value
Returns an FTODate object representing the bar's opening time.
Description
The Time
method returns the opening time of a bar at the specified shift from the current bar. The time is returned as an FTODate
object, which provides various date/time manipulation capabilities. The shift parameter determines which bar's time to return:
0: Current bar
1: Previous bar
2: Two bars ago
And so on
The timeZoneMode
parameter allows you to specify how the time should be interpreted:
TimeZoneMode.PROJECT
: Returns time in the project's timezone (default)TimeZoneMode.UTC
: Returns time in UTC
Example
// Get current bar's time in project timezone
const currentTime = this.api.Time(0);
console.log(`Current bar time: ${currentTime.toString()}`);
// Get current bar's time in UTC
const currentTimeUTC = this.api.Time(0, TimeZoneMode.UTC);
console.log(`Current bar UTC time: ${currentTimeUTC.toString()}`);
// Get previous bar's time
const previousTime = this.api.Time(1);
// Calculate time difference between bars
const timeDiff = currentTime.getTime() - previousTime.getTime();
console.log(`Time between bars: ${timeDiff} milliseconds`);
// Get opening times for last 3 bars
for (let i = 0; i < 3; i++) {
const time = this.api.Time(i);
console.log(`Bar -${i} opened at: ${time.toString()}`);
}
Last updated