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++ code involved in 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#include <time.h> 35// Define OH_JSVM_CreateDate. 36static JSVM_Value CreateDate(JSVM_Env env, JSVM_CallbackInfo info) { 37 // Obtain the number of seconds elapsed since the Unix epoch using the C function and convert the value into milliseconds. 38 double value = static_cast<double>(time(NULL) * 1000); 39 // Call OH_JSVM_CreateDate to convert the double value into a JS value indicating the date and time. 40 JSVM_Value returnValue = nullptr; 41 42 JSVM_CALL(OH_JSVM_CreateDate(env, value, &returnValue)); 43 44 bool isDate; 45 JSVM_CALL(OH_JSVM_IsDate(env, returnValue, &isDate)); 46 if (isDate == false) { 47 OH_LOG_ERROR(LOG_APP, "JSVM IsDate fail"); 48 return returnValue; 49 } 50 51 value = 0; 52 JSVM_CALL(OH_JSVM_GetDateValue(env, returnValue, &value)); 53 54 uint64_t time = static_cast<uint64_t>(value) / 1000; 55 char *date = ctime(reinterpret_cast<time_t *>(&time)); 56 OH_LOG_INFO(LOG_APP, "JSVM CreateDate success:%{public}s", date); 57 58 return returnValue; 59} 60 61// Register the CreateDate callback. 62static JSVM_CallbackStruct param[] = { 63 {.data = nullptr, .callback = CreateDate}, 64}; 65static JSVM_CallbackStruct *method = param; 66// Set a property descriptor named createDate and associate it with a callback. This allows the createDate callback to be called from JS. 67static JSVM_PropertyDescriptor descriptor[] = { 68 {"createDate", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 69}; 70// Call the C++ code from JS. 71const char *srcCallNative = R"JS(createDate())JS"; 72``` 73 74### OH_JSVM_GetDateValue 75 76Use **OH_JSVM_GetDateValue** to obtain the C double primitive of the time value for the given JS **Date** object. 77 78CPP code: 79 80```cpp 81#include <time.h> 82// Define OH_JSVM_GetDateValue. 83static JSVM_Value GetDateValue(JSVM_Env env, JSVM_CallbackInfo info) { 84 size_t argc = 1; 85 JSVM_Value args[1] = {nullptr}; 86 JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr)); 87 // Obtain the Unix timestamp passed in. 88 double value; 89 JSVM_CALL(OH_JSVM_GetDateValue(env, args[0], &value)); 90 91 // Convert the obtained Unix Time Stamp time into a date string for printing. 92 uint64_t time = static_cast<uint64_t>(value) / 1000; 93 char *date = ctime(reinterpret_cast<time_t *>(&time)); 94 OH_LOG_INFO(LOG_APP, "JSVM GetDateValue success:%{public}s", date); 95 96 JSVM_Value returnValue = nullptr; 97 JSVM_CALL(OH_JSVM_CreateDouble(env, value, &returnValue)); 98 return returnValue; 99} 100 101// Register the CreateDate callback. 102static JSVM_CallbackStruct param[] = { 103 {.data = nullptr, .callback = GetDateValue}, 104}; 105static JSVM_CallbackStruct *method = param; 106// Set a property descriptor named createDate and associate it with a callback. This allows the createDate callback to be called from JS. 107static JSVM_PropertyDescriptor descriptor[] = { 108 {"getDateValue", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 109}; 110// Call the C++ code from JS. 111const char *srcCallNative = R"JS(getDateValue(new Date(Date.now())))JS"; 112``` 113 114### OH_JSVM_IsDate 115 116Use **OH_JSVM_IsDate** to check whether a JS object is a date. 117 118CPP code: 119 120```cpp 121// Define OH_JSVM_IsDate. 122static JSVM_Value IsDate(JSVM_Env env, JSVM_CallbackInfo info) { 123 size_t argc = 1; 124 JSVM_Value args[1] = {nullptr}; 125 JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr)); 126 bool isData; 127 JSVM_CALL(OH_JSVM_IsDate(env, args[0], &isData)); 128 OH_LOG_INFO(LOG_APP, "JSVM IsDate success:%{public}d", isData); 129 130 JSVM_Value result = nullptr; 131 JSVM_CALL(OH_JSVM_GetBoolean(env, isData, &result)); 132 return result; 133} 134// Register the CreateDate callback. 135static JSVM_CallbackStruct param[] = { 136 {.data = nullptr, .callback = IsDate}, 137}; 138static JSVM_CallbackStruct *method = param; 139// Set a property descriptor named createDate and associate it with a callback. This allows the createDate callback to be called from JS. 140static JSVM_PropertyDescriptor descriptor[] = { 141 {"isDate", nullptr, method, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 142}; 143// Call the C++ code from JS. 144const char *srcCallNative = R"JS(isDate(new Date(Date.now())))JS"; 145``` 146