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