• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Working with Date Using JSVM-API
2
3## Introduction
4
5JSVM-API provides APIs for processing JavaScript (JS) **Date** objects in C/C++. These APIs are useful for working with time- and date-related logic in the JSVM module.
6
7## Basic Concepts
8
9In JSVM-API, the value of a JS **Date** object is the number of milliseconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970).
10
11The JS **Date** object provides a way to represent and manage date and time in JS. With the **Date** object, you can create an object that represents a specific moment, perform date- and time-related calculations (such as adding or subtracting time intervals), and format date as a string for display.
12
13With the functions for interacting with the **Date** object, the JSVM module can be closely integrated with the JS environment to perform more complex date- and time-related operations.
14
15## Available APIs
16
17| API                      | Description                      |
18|----------------------------|--------------------------------|
19| OH_JSVM_CreateDate           | Creates a **Date** object representing the given number of milliseconds. |
20| OH_JSVM_GetDateValue        | Obtains the C double primitive of the time value for the given JS **Date** object. |
21| OH_JSVM_IsDate               | Checks whether a JS object is a date.|
22
23## Example
24
25If you are just starting out with JSVM-API, see [JSVM-API Development Process](use-jsvm-process.md). The following demonstrates only the C++ and ArkTS code for date management.
26
27### OH_JSVM_CreateDate
28
29Use **OH_JSVM_IsTypedarray** to create a **Date** object representing the given number of milliseconds.
30
31CPP code:
32
33```cpp
34// hello.cpp
35#include "napi/native_api.h"
36#include "ark_runtime/jsvm.h"
37#include <hilog/log.h>
38// Register the CreateDate callback.
39static JSVM_CallbackStruct param[] = {
40    {.data = nullptr, .callback = CreateDate},
41};
42static JSVM_CallbackStruct *method = param;
43// Set a property descriptor named createDate and associate it with a callback. This allows the createDate callback to be called from JS.
44static JSVM_PropertyDescriptor descriptor[] = {
45    {"createDate", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
46};
47// Define OH_JSVM_CreateDate.
48static JSVM_Value CreateDate(JSVM_Env env, JSVM_CallbackInfo info) {
49    double value = 1501924876711;
50    // Call OH_JSVM_CreateDate to convert the double value into a JS value indicating the date and time.
51    JSVM_Value returnValue = nullptr;
52    JSVM_Status status = OH_JSVM_CreateDate(env, value, &returnValue);
53    if (status != JSVM_OK) {
54        OH_LOG_ERROR(LOG_APP, "JSVM CreateDate fail");
55    } else {
56        OH_LOG_INFO(LOG_APP, "JSVM CreateDate success");
57    }
58    return returnValue;
59}
60```
61
62ArkTS code:
63
64```ts
65import hilog from "@ohos.hilog"
66// Import the native APIs.
67import napitest from "libentry.so"
68let script: string = `createDate()`;
69try {
70  let result = napitest.runJsVm(script);
71  hilog.info(0x0000, 'testJSVM', 'Test JSVM CreateDate: %{public}s', result);
72} catch (error) {
73  hilog.error(0x0000, 'testJSVM', 'Test JSVM CreateDate error: %{public}s', error.message);
74}
75```
76
77### OH_JSVM_GetDateValue
78
79Use **OH_JSVM_GetDateValue** to obtain the C double primitive of the time value for the given JS **Date** object.
80
81CPP code:
82
83```cpp
84// hello.cpp
85#include "napi/native_api.h"
86#include "ark_runtime/jsvm.h"
87#include <hilog/log.h>
88// Register the GetDateValue callback.
89static JSVM_CallbackStruct param[] = {
90    {.data = nullptr, .callback = GetDateValue},
91};
92static JSVM_CallbackStruct *method = param;
93// Set a property descriptor named getDateValue and associate it with a callback. This allows the GetDateValue callback to be called from JS.
94static JSVM_PropertyDescriptor descriptor[] = {
95    {"getDateValue", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
96};
97// Define OH_JSVM_GetDateValue.
98static JSVM_Value GetDateValue(JSVM_Env env, JSVM_CallbackInfo info)
99{
100    size_t argc = 1;
101    JSVM_Value args[1] = {nullptr};
102    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
103    // Obtain the Unix timestamp passed in.
104    double value;
105    JSVM_Status status = OH_JSVM_GetDateValue(env, args[0], &value);
106    if (status != JSVM_OK) {
107        OH_LOG_ERROR(LOG_APP, "JSVM IsArray fail");
108    } else {
109        // Print the obtained Unix timestamp.
110        OH_LOG_INFO(LOG_APP, "JSVM gets the incoming Green time:%{public}lf.", value);
111    }
112    JSVM_Value returnValue = nullptr;
113    OH_JSVM_CreateDouble(env, value, &returnValue);
114    return returnValue;
115}
116```
117
118ArkTS code:
119
120```ts
121import hilog from "@ohos.hilog"
122// Import the native APIs.
123import napitest from "libentry.so"
124let script: string = `getDateValue(new Date(Date.now()))`;
125try {
126  let result = napitest.runJsVm(script);
127  hilog.info(0x0000, 'testJSVM', 'Test JSVM GetDateValue: %{public}s', result);
128} catch (error) {
129  hilog.error(0x0000, 'testJSVM', 'Test JSVM GetDateValue error: %{public}s', error.message);
130}
131```
132
133### OH_JSVM_IsDate
134
135Use **OH_JSVM_IsDate** to check whether a JS object is a date.
136
137CPP code:
138
139```cpp
140// hello.cpp
141#include "napi/native_api.h"
142#include "ark_runtime/jsvm.h"
143#include <hilog/log.h>
144// Register the IsDate callback.
145static JSVM_CallbackStruct param[] = {
146    {.data = nullptr, .callback = IsDate},
147};
148static JSVM_CallbackStruct *method = param;
149// Set a property descriptor named isDate and associate it with a callback. This allows the IsDate callback to be called from JS.
150static JSVM_PropertyDescriptor descriptor[] = {
151    {"isDate", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
152};
153// Define OH_JSVM_IsDate.
154static JSVM_Value IsDate(JSVM_Env env, JSVM_CallbackInfo info) {
155    size_t argc = 1;
156    JSVM_Value args[1] = {nullptr};
157    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
158    bool result;
159    JSVM_Status status = OH_JSVM_IsDate(env, args[0], &result);
160    if (status != JSVM_OK) {
161        OH_LOG_ERROR(LOG_APP, "JSVM IsDate fail");
162    } else {
163        OH_LOG_INFO(LOG_APP, "JSVM IsDate success:%{public}d", result);
164    }
165    JSVM_Value isDate = nullptr;
166    OH_JSVM_GetBoolean(env, result, &isDate);
167    return isDate;
168}
169```
170
171ArkTS code:
172
173```ts
174import hilog from "@ohos.hilog"
175// Import the native APIs.
176import napitest from "libentry.so"
177try {
178  let script: string = `isDate(new Date(Date.now()))`;
179  hilog.info(0x0000, 'testJSVM', 'Test JSVM IsDate: %{public}s', napitest.runJsVm(script));
180  script = `
181      isDate(1)
182  `;
183  hilog.info(0x0000, 'testJSVM', 'Test JSVM IsDate: %{public}s', napitest.runJsVm(script));
184} catch (error) {
185  hilog.error(0x0000, 'testJSVM', 'Test JSVM IsDate error: %{public}s', error.message);
186}
187```
188