• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Managing Date Using JSVM-API
2
3## Overview
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 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 time, perform date- and time-related calculations (such as adding or subtracting time intervals), and format date as a string for display. 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
11With 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.
12
13## Available APIs
14
15| API                      | Description                      |
16|----------------------------|--------------------------------|
17| OH_JSVM_CreateDate           | Creates a **Date** object representing the given number of milliseconds. |
18| OH_JSVM_GetDateValue        | Obtains the C double primitive of the time value for the given JS **Date** object. |
19| OH_JSVM_IsDate               | Checks whether a JS object is a date.|
20
21## Example
22
23### OH_JSVM_CreateDate
24
25Create a **Date** object representing the given number of milliseconds.
26
27CPP code
28
29```cpp
30// Register a callback for CreateDate.
31static JSVM_CallbackStruct param[] = {
32    {.data = nullptr, .callback = CreateDate},
33};
34static JSVM_CallbackStruct *method = param;
35// Expose the alias of the CreateDate method to TS.
36static JSVM_PropertyDescriptor descriptor[] = {
37    {"createDate", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
38};
39// Create a Date object.
40static JSVM_Value CreateDate(JSVM_Env env, JSVM_CallbackInfo info) {
41    g_data_type = "date";
42    double value = 1501924876711;
43    // Call OH_JSVM_CreateDate to convert the double value into a JS value indicating the date and time.
44    JSVM_Value returnValue = nullptr;
45    OH_JSVM_CreateDate(env, value, &returnValue);
46    return returnValue;
47}
48```
49
50ArkTS code
51
52```ts
53let script: string = `createDate()`;
54try {
55  let result = napitest.runJsVm(script);
56  hilog.info(0x0000, 'testJSVM', 'Test JSVM CreateDate: %{public}s',  JSON.stringify(result));
57} catch (error) {
58  hilog.error(0x0000, 'testJSVM', 'Test JSVM CreateDate error: %{public}s', error.message);
59}
60```
61
62### OH_JSVM_GetDateValue
63
64Obtain the C double primitive of the time value for the given JS **Date** object.
65
66CPP code
67
68```cpp
69// Register a callback for GetDateValue.
70static JSVM_CallbackStruct param[] = {
71    {.data = nullptr, .callback = GetDateValue},
72};
73static JSVM_CallbackStruct *method = param;
74// Expose the alias of the GetDateValue method to TS.
75static JSVM_PropertyDescriptor descriptor[] = {
76    {"getDateValue", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
77};
78// Call GetDateValue to obtain the C double primitive of the time value.
79static JSVM_Value GetDateValue(JSVM_Env env, JSVM_CallbackInfo info) {
80    g_data_type = "double";
81    size_t argc = 1;
82    JSVM_Value args[1] = {nullptr};
83    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
84    // Obtain the Unix timestamp passed in.
85    double value;
86    JSVM_Status status = OH_JSVM_GetDateValue(env, args[0], &value);
87    if (status != JSVM_OK) {
88        OH_JSVM_ThrowError(env, nullptr, "OH_JSVM_GetDateValue fail");
89        return nullptr;
90    }
91    // Print the obtained Unix timestamp.
92    OH_LOG_INFO(LOG_APP, "JSVM gets the incoming Green time:%{public}lf.", value);
93    JSVM_Value returnValue = nullptr;
94    OH_JSVM_CreateDouble(env, value, &returnValue);
95    return returnValue;
96}
97```
98
99ArkTS code
100
101```ts
102let script: string = `getDateValue(new Date(Date.now()))`;
103try {
104  let result = napitest.runJsVm(script);
105  hilog.info(0x0000, 'testJSVM', 'Test JSVM GetDateValue: %{public}s',  JSON.stringify(result));
106} catch (error) {
107  hilog.error(0x0000, 'testJSVM', 'Test JSVM GetDateValue error: %{public}s', error.message);
108}
109```
110
111### OH_JSVM_IsDate
112
113Check whether a JS object is a date.
114
115CPP code
116
117```cpp
118// Register a callback for IsDate.
119static JSVM_CallbackStruct param[] = {
120    {.data = nullptr, .callback = IsDate},
121};
122static JSVM_CallbackStruct *method = param;
123// Expose the alias of the IsDate method to TS.
124static JSVM_PropertyDescriptor descriptor[] = {
125    {"isDate", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
126};
127// Call IsDate to check whether the JS object indicates a date.
128static JSVM_Value IsDate(JSVM_Env env, JSVM_CallbackInfo info) {
129    size_t argc = 1;
130    JSVM_Value args[1] = {nullptr};
131    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
132
133    bool result;
134    OH_JSVM_IsDate(env, args[0], &result);
135
136    JSVM_Value isDate = nullptr;
137    OH_JSVM_GetBoolean(env, result, &isDate);
138    return isDate;
139}
140```
141
142ArkTS code
143
144```ts
145try {
146  let script: string = `isDate(new Date(Date.now()))`;
147  hilog.info(0x0000, 'testJSVM', 'Test JSVM IsDate: %{public}s',  JSON.stringify(napitest.runJsVm(script)));
148  script = `
149      isDate(1)
150  `;
151  hilog.info(0x0000, 'testJSVM', 'Test JSVM IsDate: %{public}s', JSON.stringify(napitest.runJsVm(script)));
152} catch (error) {
153  hilog.error(0x0000, 'testJSVM', 'Test JSVM IsDate error: %{public}s', error.message);
154}
155```
156