Repository: nodejs/node-addon-api (2026-01-30T17:18:05Z)

Issue:
Add `Date::New` for `std::chrono::system_clock::time_point`
It would be nice to be able to create a `Date` from a `std::chrono::system_clock::time_point`

Here is my (not so great) implementation):

```cpp
Napi::Value timePointToJsDate(Napi::Env env, std::chrono::system_clock::time_point tp)
{
    using namespace std::chrono;
    auto ms static_cast<double>(duration_cast<milliseconds>(tp.time_since_epoch()).count());
    return Date::New(env, ms);
}
```


Discussion:
@cacharle thanks for the issue! Would you like to file a PR for this?
ye, sure thing :)



Patch:
diff --git a/doc/date.md b/doc/date.md
index 4c5fefa5e..7131b016f 100644
--- a/doc/date.md
+++ b/doc/date.md
@@ -37,6 +37,20 @@ static Napi::Date Napi::Date::New(Napi::Env env, double value);
 
 Returns a new instance of `Napi::Date` object.
 
+### New
+
+Creates a new instance of a `Napi::Date` object.
+
+```cpp
+static Napi::Date Napi::Date::New(napi_env env, std::chrono::system_clock::time_point time_point);
+```
+
+ - `[in] env`: The environment in which to construct the `Napi::Date` object.
+ - `[in] value`: The point in time, represented by an
+   `std::chrono::system_clock::time_point`.
+
+Returns a new instance of `Napi::Date` object.
+
 ### ValueOf
 
 ```cpp
diff --git a/napi-inl.h b/napi-inl.h
index 0f1717ecd..31c781885 100644
--- a/napi-inl.h
+++ b/napi-inl.h
@@ -1199,6 +1199,13 @@ inline Date Date::New(napi_env env, double val) {
   return Date(env, value);
 }
 
+inline Date Date::New(napi_env env, std::chrono::system_clock::time_point tp) {
+  using namespace std::chrono;
+  auto ms = static_cast<double>(
+      duration_cast<milliseconds>(tp.time_since_epoch()).count());
+  return Date::New(env, ms);
+}
+
 inline void Date::CheckCast(napi_env env, napi_value value) {
   NAPI_CHECK(value != nullptr, "Date::CheckCast", "empty value");
 
diff --git a/napi.h b/napi.h
index 013a9114d..4b015a0e6 100644
--- a/napi.h
+++ b/napi.h
@@ -17,6 +17,7 @@
 #if NAPI_HAS_THREADS
 #include <mutex>
 #endif  // NAPI_HAS_THREADS
+#include <chrono>
 #include <string>
 #include <vector>
 
@@ -685,6 +686,12 @@ class Date : public Value {
                   double value   ///< Number value
   );
 
+  /// Creates a new Date value from a std::chrono::system_clock::time_point.
+  static Date New(
+      napi_env env,  ///< Node-API environment
+      std::chrono::system_clock::time_point time_point  ///< Time point value
+  );
+
   static void CheckCast(napi_env env, napi_value value);
 
   Date();  ///< Creates a new _empty_ Date instance.


Test patch:
diff --git a/test/date.cc b/test/date.cc
index efd433af4..a446bde22 100644
--- a/test/date.cc
+++ b/test/date.cc
@@ -11,6 +11,11 @@ Value CreateDate(const CallbackInfo& info) {
   return Date::New(info.Env(), input);
 }
 
+Value CreateDateFromTimePoint(const CallbackInfo& info) {
+  auto input = std::chrono::system_clock::time_point{};
+  return Date::New(info.Env(), input);
+}
+
 Value IsDate(const CallbackInfo& info) {
   Date input = info[0].As<Date>();
 
@@ -35,6 +40,8 @@ Value OperatorValue(const CallbackInfo& info) {
 Object InitDate(Env env) {
   Object exports = Object::New(env);
   exports["CreateDate"] = Function::New(env, CreateDate);
+  exports["CreateDateFromTimePoint"] =
+      Function::New(env, CreateDateFromTimePoint);
   exports["IsDate"] = Function::New(env, IsDate);
   exports["ValueOf"] = Function::New(env, ValueOf);
   exports["OperatorValue"] = Function::New(env, OperatorValue);
diff --git a/test/date.js b/test/date.js
index 86c8af6ac..588b741b1 100644
--- a/test/date.js
+++ b/test/date.js
@@ -9,9 +9,11 @@ function test (binding) {
     CreateDate,
     IsDate,
     ValueOf,
-    OperatorValue
+    OperatorValue,
+    CreateDateFromTimePoint
   } = binding.date;
   assert.deepStrictEqual(CreateDate(0), new Date(0));
+  assert.deepStrictEqual(CreateDateFromTimePoint(), new Date(0));
   assert.strictEqual(IsDate(new Date(0)), true);
   assert.strictEqual(ValueOf(new Date(42)), 42);
   assert.strictEqual(OperatorValue(new Date(42)), true);