js-apis-system-time.md 16.0 KB
Newer Older
E
ester.zhou 已提交
1
# System Time and Time Zone
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
The **systemTime** module provides system time and time zone features. You can use the APIs of this module to set and obtain the system time and time zone.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6 7
> **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.
>- The APIs of this module are system APIs and cannot be called by third-party applications.
Z
zengyawen 已提交
8

W
wusongqing 已提交
9
## Modules to Import
Z
zengyawen 已提交
10

E
ester.zhou 已提交
11

Z
zengyawen 已提交
12 13 14 15 16
```
import systemTime from '@ohos.systemTime';
```


W
wusongqing 已提交
17
## systemTime.setTime
Z
zengyawen 已提交
18

W
wusongqing 已提交
19 20 21 22 23 24
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 已提交
25
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
26

E
ester.zhou 已提交
27
**Parameters**
W
wusongqing 已提交
28

E
ester.zhou 已提交
29 30 31 32
| Name  | Type                     | Mandatory| Description                                      |
| -------- | ------------------------- | ---- | ------------------------------------------ |
| time     | number                    | Yes  | Timestamp to set, in milliseconds. |
| callback | AsyncCallback<void> | Yes  | Callback used to return the result.|
E
ester.zhou 已提交
33 34 35 36

**Example**

  ```js
E
ester.zhou 已提交
37
  // Set the system time to 2021-01-20 02:36:25.
W
wusongqing 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  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 已提交
57
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
58

E
ester.zhou 已提交
59
**Parameters**
W
wusongqing 已提交
60

E
ester.zhou 已提交
61 62 63
| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
| time   | number | Yes  | Timestamp to set, in milliseconds. |
W
wusongqing 已提交
64

E
ester.zhou 已提交
65 66
**Return value**

E
ester.zhou 已提交
67 68
| Type               | Description                |
| ------------------- | -------------------- |
E
ester.zhou 已提交
69 70 71 72 73
| Promise<void> | Promise used to return the result.|

**Example**

  ```js
E
ester.zhou 已提交
74 75 76 77 78 79 80
  // 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));
  });
W
wusongqing 已提交
81 82 83 84 85 86 87 88 89
  ```


## 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 已提交
90
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
91

E
ester.zhou 已提交
92
**Parameters**
W
wusongqing 已提交
93

E
ester.zhou 已提交
94 95 96 97
| Name  | Type                       | Mandatory| Description                                                        |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| isNano   | boolean                     | No  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds.<br>- **false**: in milliseconds. |
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.        |
E
ester.zhou 已提交
98 99 100 101

**Example**

  ```js
W
wusongqing 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  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 已提交
118
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
119

E
ester.zhou 已提交
120
**Parameters**
W
wusongqing 已提交
121

E
ester.zhou 已提交
122 123 124
| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
| isNano | boolean | No  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds.<br>- **false**: in milliseconds. |
W
wusongqing 已提交
125

E
ester.zhou 已提交
126 127
**Return value**

E
ester.zhou 已提交
128 129
| Type                 | Description                                                        |
| --------------------- | ------------------------------------------------------------ |
E
ester.zhou 已提交
130 131 132 133 134
| Promise&lt;number&gt; | Promise used to return the time.|

**Example**

  ```js
W
wusongqing 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148
  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 已提交
149
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
150

E
ester.zhou 已提交
151
**Parameters**
W
wusongqing 已提交
152

E
ester.zhou 已提交
153 154 155 156
| Name  | Type                       | Mandatory| Description                                                        |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| isNano   | boolean                     | No  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds.<br>- **false**: in milliseconds. |
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.|
E
ester.zhou 已提交
157 158 159 160

**Example**

  ```js
W
wusongqing 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  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 已提交
177
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
178

E
ester.zhou 已提交
179
**Parameters**
W
wusongqing 已提交
180

E
ester.zhou 已提交
181 182 183
| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
| isNano | boolean | No  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds.<br>- **false**: in milliseconds. |
W
wusongqing 已提交
184

E
ester.zhou 已提交
185 186
**Return value**

E
ester.zhou 已提交
187 188
| Type                 | Description                                                        |
| --------------------- | ------------------------------------------------------------ |
E
ester.zhou 已提交
189 190 191 192 193
| Promise&lt;number&gt; | Promise used to return the time.|

**Example**

  ```js
E
ester.zhou 已提交
194
  systemTime.getRealActiveTime().then((data) => {
W
wusongqing 已提交
195 196 197 198 199 200 201 202 203
      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>

E
ester.zhou 已提交
204
getRealTime(isNano?: boolean, callback: AsyncCallback&lt;number&gt;): void
W
wusongqing 已提交
205 206 207

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 已提交
208
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
209

E
ester.zhou 已提交
210
**Parameters**
W
wusongqing 已提交
211

E
ester.zhou 已提交
212 213 214 215
| Name  | Type                       | Mandatory| Description                                                        |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| isNano   | boolean                     | No  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds.<br>- **false**: in milliseconds. |
| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the time.  |
E
ester.zhou 已提交
216 217 218 219

**Example**

  ```js
W
wusongqing 已提交
220 221 222 223 224 225 226 227 228 229 230 231
  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>

E
ester.zhou 已提交
232
getRealTime(isNano?: boolean): Promise&lt;number&gt;
W
wusongqing 已提交
233 234 235

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

E
ester.zhou 已提交
236
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
237

E
ester.zhou 已提交
238
**Parameters**
W
wusongqing 已提交
239

E
ester.zhou 已提交
240 241 242
| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ---- | ------------------------------------------------------------ |
| isNano | boolean | No  | Whether the time to return is in nanoseconds.<br>- **true**: in nanoseconds.<br>- **false**: in milliseconds. |
W
wusongqing 已提交
243

E
ester.zhou 已提交
244 245
**Return value**

E
ester.zhou 已提交
246 247
| Type                 | Description                                                        |
| --------------------- | ------------------------------------------------------------ |
E
ester.zhou 已提交
248 249 250 251 252
| Promise&lt;number&gt; | Promise used to return the time.|

**Example**

  ```js
W
wusongqing 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  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 已提交
269
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
270

E
ester.zhou 已提交
271
**Parameters**
W
wusongqing 已提交
272

E
ester.zhou 已提交
273 274 275 276
| Name  | Type                     | Mandatory| Description                                      |
| -------- | ------------------------- | ---- | ------------------------------------------ |
| date     | Date                      | Yes  | Target date to set.                                |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
E
ester.zhou 已提交
277 278 279 280

**Example**

  ```js
W
wusongqing 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  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 已提交
300
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
301

E
ester.zhou 已提交
302
**Parameters**
W
wusongqing 已提交
303

E
ester.zhou 已提交
304 305 306
| Name| Type| Mandatory| Description      |
| ------ | ---- | ---- | ---------- |
| date   | Date | Yes  | Target date to set.|
W
wusongqing 已提交
307

E
ester.zhou 已提交
308 309
**Return value**

E
ester.zhou 已提交
310 311
| Type               | Description                |
| ------------------- | -------------------- |
E
ester.zhou 已提交
312 313 314 315 316
| Promise&lt;void&gt; | Promise used to return the result.|

**Example**

  ```js
W
wusongqing 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  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 已提交
332
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
333

E
ester.zhou 已提交
334
**Parameters**
W
wusongqing 已提交
335

E
ester.zhou 已提交
336 337 338
| Name  | Type                     | Mandatory| Description                        |
| -------- | ------------------------- | ---- | ---------------------------- |
| callback | AsyncCallback&lt;Date&gt; | Yes  | Callback used to return the current system date.|
E
ester.zhou 已提交
339 340 341 342

**Example**

  ```js
W
wusongqing 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  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 已提交
359
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
360

E
ester.zhou 已提交
361
**Return value**
W
wusongqing 已提交
362

E
ester.zhou 已提交
363 364
| Type               | Description                                     |
| ------------------- | ----------------------------------------- |
E
ester.zhou 已提交
365 366 367 368 369
| Promise&lt;Date&gt; | Promise used to return the current system date.|

**Example**

  ```js
W
wusongqing 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  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 已提交
386
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
387

E
ester.zhou 已提交
388
**Parameters**
W
wusongqing 已提交
389

E
ester.zhou 已提交
390 391 392 393
| Name  | Type                     | Mandatory| Description                                      |
| -------- | ------------------------- | ---- | ------------------------------------------ |
| timezone | string                    | Yes  | System time zone to set.                                |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
E
ester.zhou 已提交
394 395 396 397

**Example**

  ```js
W
wusongqing 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
  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 已提交
416
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
417

E
ester.zhou 已提交
418
**Parameters**
W
wusongqing 已提交
419

E
ester.zhou 已提交
420 421 422
| Name  | Type  | Mandatory| Description      |
| -------- | ------ | ---- | ---------- |
| timezone | string | Yes  | System time zone to set.|
W
wusongqing 已提交
423

E
ester.zhou 已提交
424 425
**Return value**

E
ester.zhou 已提交
426 427
| Type               | Description                |
| ------------------- | -------------------- |
E
ester.zhou 已提交
428 429 430 431 432
| Promise&lt;void&gt; | Promise used to return the result.|

**Example**

  ```js
W
wusongqing 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446
  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 已提交
447
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
448

E
ester.zhou 已提交
449
**Parameters**
W
wusongqing 已提交
450

E
ester.zhou 已提交
451 452 453
| Name  | Type                       | Mandatory| Description                    |
| -------- | --------------------------- | ---- | ------------------------ |
| callback | AsyncCallback&lt;string&gt; | Yes  | Callback used to return the system time zone.|
E
ester.zhou 已提交
454 455 456 457

**Example**

  ```js
W
wusongqing 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  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 已提交
474
**System capability**: SystemCapability.MiscServices.Time
W
wusongqing 已提交
475

E
ester.zhou 已提交
476
**Return value**
W
wusongqing 已提交
477

E
ester.zhou 已提交
478 479
| Type                 | Description                                 |
| --------------------- | ------------------------------------- |
E
ester.zhou 已提交
480 481 482 483 484
| Promise&lt;string&gt; | Promise used to return the system time zone.|

**Example**

  ```js
W
wusongqing 已提交
485 486 487 488 489 490
  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));
  });
  ```