js-apis-system-time.md 14.1 KB
Newer Older
W
wusongqing 已提交
1
# Setting the System Time
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
5 6


W
wusongqing 已提交
7
## Modules to Import
Z
zengyawen 已提交
8

W
wusongqing 已提交
9
  
Z
zengyawen 已提交
10 11 12 13 14
```
import systemTime from '@ohos.systemTime';
```


W
wusongqing 已提交
15
## systemTime.setTime
Z
zengyawen 已提交
16

W
wusongqing 已提交
17 18 19 20 21 22
setTime(time : number, callback : AsyncCallback<void>) : void

Sets the system time. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
23
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | time | number | Yes| Timestamp to set, in milliseconds.| 
  | callback | AsyncCallback<void> | Yes| Callback used to process the received return value.| 

- Example
    
  ```
  // Set the system time to 2021-01-20 02:36:25.
  var time = 1611081385000;
  systemTime.setTime(time, (error, data) => {
      if (error) {
          console.error(`failed to systemTime.setTime because ` + JSON.stringify(error));
          return;
      }
      console.log(`systemTime.setTime success data : ` + JSON.stringify(data));
  });
  ```


## systemTime.setTime

setTime(time : number) : Promise<void>

Sets the system time. This API uses a promise to return the result.

**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
54
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | time | number | Yes| Timestamp to set, in milliseconds.| 

- Return value
    | Type| Description| 
  | -------- | -------- |
  | Promise<void> | Promise used to return the result.| 

- Example
    
  ```
  // Set the system time to 2021-01-20 02:36:25.
  var time = 1611081385000;
  systemTime.setTime(time).then((data) => {
      console.log(`systemTime.setTime success data : ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to systemTime.setTime because ` + JSON.stringify(error));
  });
  ```


## systemTime.getCurrentTime<sup>8+</sup>

getCurrentTime(isNano?: boolean, callback: AsyncCallback&lt;number&gt;): void

Obtains the time elapsed since the Unix epoch. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
85
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | isNano | boolean | No| Whether nanoseconds or milliseconds will be returned. If the value is **true**, nanoseconds will be returned. Otherwise, milliseconds will be returned.| 
  | callback | AsyncCallback&lt;number&gt; | Yes| Callback used to return the time.| 

- Example
    
  ```
  systemTime.getCurrentTime(true, (error, data) => {
      if (error) {
          console.error(`failed to systemTime.getCurrentTime because ` + JSON.stringify(error));
          return;
      }
      console.log(`systemTime.getCurrentTime success data : ` + JSON.stringify(data));
  });
  ```


## systemTime.getCurrentTime<sup>8+</sup>

getCurrentTime(isNano?: boolean): Promise&lt;number&gt;

Obtains the time elapsed since the Unix epoch. This API uses a promise to return the result.

E
ester.zhou 已提交
112
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | isNano | boolean | No| Whether nanoseconds or milliseconds will be returned. If the value is **true**, nanoseconds will be returned. Otherwise, milliseconds will be returned.| 

- Return value
    | Type| Description| 
  | -------- | -------- |
  | Promise&lt;number&gt; | Promise used to return the time.| 

- Example
    
  ```
  systemTime.getCurrentTime().then((data) => {
      console.log(`systemTime.getCurrentTime success data : ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to systemTime.getCurrentTime because ` + JSON.stringify(error));
  });
  ```


## systemTime.getRealActiveTime<sup>8+</sup>

getRealActiveTime(isNano?: boolean, callback: AsyncCallback&lt;number&gt;): void

Obtains the time elapsed since system start, excluding the deep sleep time. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
141
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | isNano | boolean | No| Whether nanoseconds or milliseconds will be returned. If the value is **true**, nanoseconds will be returned. Otherwise, milliseconds will be returned.| 
  | callback | AsyncCallback&lt;number&gt; | Yes| Callback used to return the time.| 

- Example
    
  ```
  systemTime.getRealActiveTime(true, (error, data) => {
      if (error) {
          console.error(`failed to systemTime.getRealActiveTimebecause ` + JSON.stringify(error));
          return;
      }
      console.log(`systemTime.getRealActiveTime success data : ` + JSON.stringify(data));
  });
  ```


## systemTime.getRealActiveTime<sup>8+</sup>

getRealActiveTime(isNano?: boolean): Promise&lt;number&gt;

Obtains the time elapsed since system start, excluding the deep sleep time. This API uses a promise to return the result.

E
ester.zhou 已提交
168
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | isNano | boolean | No| Whether nanoseconds or milliseconds will be returned. If the value is **true**, nanoseconds will be returned. Otherwise, milliseconds will be returned.| 

- Return value
    | Type| Description| 
  | -------- | -------- |
  | Promise&lt;number&gt; | Promise used to return the time.| 

- Example
    
  ```
  systemTime.getCurrentTime().then((data) => {
      console.log(`systemTime.getRealActiveTime success data : ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to systemTime.getRealActiveTime because ` + JSON.stringify(error));
  });
  ```


## systemTime.getRealTime<sup>8+</sup>

getRealTime(callback: AsyncCallback&lt;number&gt;): void

Obtains the time elapsed since system start, including the deep sleep time. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
197
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | isNano | boolean | No| Whether nanoseconds or milliseconds will be returned. If the value is **true**, nanoseconds will be returned. Otherwise, milliseconds will be returned.| 
  | callback | AsyncCallback&lt;number&gt; | Yes| Callback used to return the time.| 

- Example
    
  ```
  systemTime.getRealTime(true, (error, data) => {
      if (error) {
          console.error(`failed to systemTime.getRealTime because ` + JSON.stringify(error));
          return;
      }
      console.log(`systemTime.getRealTime success data: ` + JSON.stringify(data));
  });
  ```


## systemTime.getRealTime<sup>8+</sup>

getRealTime(): Promise&lt;number&gt;

Obtains the time elapsed since system start, including the deep sleep time. This API uses a promise to return the result.

E
ester.zhou 已提交
224
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | isNano | boolean | No| Whether nanoseconds or milliseconds will be returned. If the value is **true**, nanoseconds will be returned. Otherwise, milliseconds will be returned.| 

- Return value
    | Type| Description| 
  | -------- | -------- |
  | Promise&lt;number&gt; | Promise used to return the time.| 

- Example
    
  ```
  systemTime.getRealTime().then((data) => {
      console.log(`systemTime.getRealTime success data: ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to systemTime.getRealTime because ` + JSON.stringify(error));
  });
  ```


## systemTime.setDate

setDate(date: Date, callback: AsyncCallback&lt;void&gt;): void

Sets the system date. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
255
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | date | Date | Yes| Target date to set.| 
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to process the received return value.| 

- Example
    
  ```
  var data = new Date("October 13, 2020 11:13:00");
  systemTime.setDate(data,(error, data) => {       
      if (error) {            
      console.error('failed to systemTime.setDate because ' + JSON.stringify(error));           
      return;       
  }        
      console.info('systemTime.setDate success data : ' + JSON.stringify(data));    
  });
  ```


## systemTime.setDate

setDate(date: Date): Promise&lt;void&gt;

Sets the system date. This API uses a promise to return the result.

**Required permissions**: ohos.permission.SET_TIME

E
ester.zhou 已提交
285
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | date | Date | Yes| Target date to set.| 

- Return value
    | Type| Description| 
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.| 

- Example
    
  ```
  var data = new Date("October 13, 2020 11:13:00"); 
  systemTime.setDate(data).then((value) => {        
      console.log(`systemTime.setDate success data : ` + JSON.stringify(value));    
  }).catch((error) => {        
      console.error(`failed to systemTime.setDate because: ` + JSON.stringify(error));
  });
  ```


## systemTime.getDate<sup>8+</sup>

getDate(callback: AsyncCallback&lt;Date&gt;): void

Obtains the current system date. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
315
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;Date&gt; | Yes| Callback used to return the current system date.| 

- Example
    
  ```
  systemTime.getDate((error, data) => {
      if (error) {
          console.error(`failed to systemTime.getDate because ` + JSON.stringify(error));
          return;
      }
      console.log(`systemTime.getDate success data : ` + JSON.stringify(data));
  });
  ```


## systemTime.getDate<sup>8+</sup>

getDate(): Promise&lt;Date&gt;

Obtains the current system date. This API uses a promise to return the result.

E
ester.zhou 已提交
341
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

- Return value
    | Type| Description| 
  | -------- | -------- |
  | Promise&lt;Date&gt; | Promise used to return the current system date.| 

- Example
    
  ```
  systemTime.getDate().then((data) => {
      console.log(`systemTime.getDate success data : ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to systemTime.getDate because ` + JSON.stringify(error));
  });
  ```


## systemTime.setTimezone

setTimezone(timezone: string, callback: AsyncCallback&lt;void&gt;): void

Sets the system time zone. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.SET_TIME_ZONE

E
ester.zhou 已提交
367
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | timezone | string | Yes| System time zone to set.| 
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to process the received return value.| 

- Example
    
  ```
  systemTime.setTimezone('Asia/Shanghai', (error, data) => {       
      if (error) {          
          console.error('failed to systemTime.setTimezone because ' + JSON.stringify(error));         
          return;       
      }       
      console.info('SystemTimePlugin systemTime.setTimezone success data : ' + JSON.stringify(data)); 
  });
  ```


## systemTime.setTimezone

setTimezone(timezone: string): Promise&lt;void&gt;

Sets the system time zone. This API uses a promise to return the result.

**Required permissions**: ohos.permission.SET_TIME_ZONE

E
ester.zhou 已提交
396
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | timezone | string | Yes| System time zone to set.| 

- Return value
    | Type| Description| 
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.| 

- Example
    
  ```
  systemTime.setTimezone('Asia/Shanghai').then((data) => {        
      console.log(`systemTime.setTimezone success data : ` + JSON.stringify(data));     
  }).catch((error) => {        
      console.error(`failed to systemTime.setTimezone because: ` + JSON.stringify(error));    
  });
  ```


## systemTime.getTimezone<sup>8+</sup>

getTimezone(callback: AsyncCallback&lt;string&gt;): void

Obtains the system time zone. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
425
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

- **Parameters**
    | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the system time zone.| 

- Example
    
  ```
  systemTime.getTimezone((error, data) => {
      if (error) {
          console.error(`failed to systemTime.getTimezone because ` + JSON.stringify(error));
          return;
      }
      console.log(`systemTime.getTimezone success data : ` + JSON.stringify(data));
  });
  ```


## systemTime.getTimezone<sup>8+</sup>

getTimezone(): Promise&lt;string&gt;

Obtains the system time zone. This API uses a promise to return the result.

E
ester.zhou 已提交
451
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

- Return value
    | Type| Description| 
  | -------- | -------- |
  | Promise&lt;string&gt; | Promise used to return the system time zone.| 

- Example
    
  ```
  systemTime.getTimezone().then((data) => {
      console.log(`systemTime.getTimezone success data : ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to systemTime.getTimezone because ` + JSON.stringify(error));
  });
  ```