1e41f4b71Sopenharmony_ci# Working with Date Using Node-API
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciNode-API provides APIs for processing ArkTS **Date** objects in C/C++. These APIs are useful for working with time- and date-related logic in the ArkTS module.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Basic Concepts
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciIn Node-API, the value of a ArkTS **Date** object is the number of milliseconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970).
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciThe ArkTS **Date** object provides a way to represent and manage date and time in ArkTS. 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.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciWith the functions for interacting with the **Date** object, the JSVM module can be closely integrated with the ArkTS environment to perform more complex date- and time-related operations.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## Available APIs
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciThe following table lists the APIs for manipulating ArkTS date in C/C++.  
18e41f4b71Sopenharmony_ci| API| Description|
19e41f4b71Sopenharmony_ci| -------- | -------- |
20e41f4b71Sopenharmony_ci| napi_create_date | Creates an ArkTS **Date** object.|
21e41f4b71Sopenharmony_ci| napi_get_date_value | Obtains the C equivalent of the given ArkTS **Date** object.|
22e41f4b71Sopenharmony_ci| napi_is_date | Checks whether the given ArkTS value is a **Date** object. You can use this API to check the type of the parameter passed from ArkTS.|
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci## Example
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ciIf you are just starting out with Node-API, see [Node-API Development Process](use-napi-process.md). The following demonstrates only the C++ and ArkTS code related to date management.
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci### napi_create_date
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ciUse **napi_create_date** to create an ArkTS **Date** instance from a C++ double value.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ciCPP code:
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci```cpp
35e41f4b71Sopenharmony_ci#include "napi/native_api.h"
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_cistatic napi_value CreateDate(napi_env env, napi_callback_info info)
38e41f4b71Sopenharmony_ci{
39e41f4b71Sopenharmony_ci    // Obtain the Unix timestamp passed in.
40e41f4b71Sopenharmony_ci    double value = 1501924876711;
41e41f4b71Sopenharmony_ci    // Call napi_create_date to convert the double value to date and time, create an ArkTS object, and place the object in returnValue.
42e41f4b71Sopenharmony_ci    napi_value returnValue = nullptr;
43e41f4b71Sopenharmony_ci    napi_create_date(env, value, &returnValue);
44e41f4b71Sopenharmony_ci    return returnValue;
45e41f4b71Sopenharmony_ci}
46e41f4b71Sopenharmony_ci```
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ciAPI declaration:
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci```ts
51e41f4b71Sopenharmony_ci// index.d.ts
52e41f4b71Sopenharmony_ciexport const createDate: () => Date;
53e41f4b71Sopenharmony_ci```
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ciArkTS code:
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci```ts
58e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
59e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_cihilog.info(0x0000, 'testTag', 'Test Node-API napi_create_date: %{public}s', testNapi.createDate().toString());
62e41f4b71Sopenharmony_ci```
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci### napi_get_date_value
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciUse **napi_get_date_value** to obtain the C++ double equivalent of the given ArkTS **Date** object.
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ciCPP code:
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci```cpp
71e41f4b71Sopenharmony_ci#include <hilog/log.h>
72e41f4b71Sopenharmony_ci#include "napi/native_api.h"
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_cistatic napi_value GetDateValue(napi_env env, napi_callback_info info)
75e41f4b71Sopenharmony_ci{
76e41f4b71Sopenharmony_ci    size_t argc = 1;
77e41f4b71Sopenharmony_ci    napi_value args[1] = {nullptr};
78e41f4b71Sopenharmony_ci    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci    // Obtain the Unix timestamp passed in.
81e41f4b71Sopenharmony_ci    double value = 0;
82e41f4b71Sopenharmony_ci    napi_status status = napi_get_date_value(env, args[0], &value);
83e41f4b71Sopenharmony_ci    if (status != napi_ok) {
84e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "napi_get_date_value fail");
85e41f4b71Sopenharmony_ci        return nullptr;
86e41f4b71Sopenharmony_ci    }
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci    // Print the obtained Unix timestamp.
89e41f4b71Sopenharmony_ci    OH_LOG_INFO(LOG_APP, "Node-API gets unix time stamp is:%{public}lf.", value);
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci    // Convert the Unix timestamp to an ArkTS double value and put it in returnValue.
92e41f4b71Sopenharmony_ci    napi_value returnValue = nullptr;
93e41f4b71Sopenharmony_ci    napi_create_double(env, value, &returnValue);
94e41f4b71Sopenharmony_ci    return returnValue;
95e41f4b71Sopenharmony_ci}
96e41f4b71Sopenharmony_ci```
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ciAPI declaration:
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci```ts
101e41f4b71Sopenharmony_ci// index.d.ts
102e41f4b71Sopenharmony_ciexport const getDateValue: (date: Date) => number | void;
103e41f4b71Sopenharmony_ci```
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ciArkTS code:
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci```ts
108e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
109e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
110e41f4b71Sopenharmony_citry {
111e41f4b71Sopenharmony_ci  const date = new Date();
112e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Node-API: output the Unix Time Stamp: %{public}d', date.getTime());
113e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_date_value: %{public}d', testNapi.getDateValue(date));
114e41f4b71Sopenharmony_ci} catch (error) {
115e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testTag', 'Test Node-API napi_get_date_value error: %{public}s', error.message);
116e41f4b71Sopenharmony_ci}
117e41f4b71Sopenharmony_ci```
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci### napi_is_date
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ciUse **napi_is_date** to check whether an ArkTS value is an ArkTS **Date** object.
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ciCPP code:
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci```cpp
126e41f4b71Sopenharmony_ci#include "napi/native_api.h"
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_cistatic napi_value IsDate(napi_env env, napi_callback_info info)
129e41f4b71Sopenharmony_ci{
130e41f4b71Sopenharmony_ci    // Obtain the parameter.
131e41f4b71Sopenharmony_ci    size_t argc = 1;
132e41f4b71Sopenharmony_ci    napi_value args[1] = {nullptr};
133e41f4b71Sopenharmony_ci    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci    // Call napi_is_date to check whether the input parameter is a Date object.
136e41f4b71Sopenharmony_ci    bool result = false;
137e41f4b71Sopenharmony_ci    napi_status status = napi_is_date(env, args[0], &result);
138e41f4b71Sopenharmony_ci    if (status != napi_ok) {
139e41f4b71Sopenharmony_ci        napi_throw_error(env, nullptr, "Node-API napi_is_date fail");
140e41f4b71Sopenharmony_ci        return nullptr;
141e41f4b71Sopenharmony_ci    }
142e41f4b71Sopenharmony_ci    // Convert the result to napi_value and return napi_value.
143e41f4b71Sopenharmony_ci    napi_value returnValue = nullptr;
144e41f4b71Sopenharmony_ci    napi_get_boolean(env, result, &returnValue);
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci    return returnValue;
147e41f4b71Sopenharmony_ci}
148e41f4b71Sopenharmony_ci```
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ciAPI declaration:
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci```ts
153e41f4b71Sopenharmony_ci// index.d.ts
154e41f4b71Sopenharmony_ciexport const isDate: <T>(date: T) => boolean | void;
155e41f4b71Sopenharmony_ci```
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ciArkTS code:
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci```ts
160e41f4b71Sopenharmony_ciimport hilog from '@ohos.hilog'
161e41f4b71Sopenharmony_ciimport testNapi from 'libentry.so'
162e41f4b71Sopenharmony_citry {
163e41f4b71Sopenharmony_ci  let now: Date = new Date();
164e41f4b71Sopenharmony_ci  let date = "123";
165e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API napi_is_date: %{public}s', testNapi.isDate(now));
166e41f4b71Sopenharmony_ci  hilog.info(0x0000, 'testTag', 'Test Node-API napi_is_date: %{public}s', testNapi.isDate(date));
167e41f4b71Sopenharmony_ci} catch (error) {
168e41f4b71Sopenharmony_ci  hilog.error(0x0000, 'testTag', 'Test Node-API napi_is_date error: %{public}s', error.message);
169e41f4b71Sopenharmony_ci}
170e41f4b71Sopenharmony_ci```
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ciTo print logs in the native CPP, add the following information to the **CMakeLists.txt** file and add the header file by using **#include "hilog/log.h"**.
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci```text
175e41f4b71Sopenharmony_ci// CMakeLists.txt
176e41f4b71Sopenharmony_ciadd_definitions( "-DLOG_DOMAIN=0xd0d0" )
177e41f4b71Sopenharmony_ciadd_definitions( "-DLOG_TAG=\"testTag\"" )
178e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libhilog_ndk.z.so)
179e41f4b71Sopenharmony_ci```
180