Fixed Date.prototype.toISOString() with invalid date values.

This fixes #373 issue on Github.
diff --git a/src/njs_date.c b/src/njs_date.c
index 58ee967..910579f 100644
--- a/src/njs_date.c
+++ b/src/njs_date.c
@@ -1136,6 +1136,8 @@
 njs_date_prototype_to_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t fmt)
 {
+    double  time;
+
     if (njs_slow_path(!njs_is_date(&args[0]))) {
         njs_type_error(vm, "cannot convert %s to date",
                        njs_type_string(args[0].type));
@@ -1143,7 +1145,14 @@
         return NJS_ERROR;
     }
 
-    return njs_date_string(vm, &vm->retval, fmt, njs_date(&args[0])->time);
+    time = njs_date(&args[0])->time;
+
+    if (fmt == NJS_DATE_FMT_TO_ISO_STRING && isnan(time)) {
+        njs_range_error(vm, "Invalid time value");
+        return NJS_ERROR;
+    }
+
+    return njs_date_string(vm, &vm->retval, fmt, time);
 }
 
 
diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c
index 1c1b0d5..760d6f9 100644
--- a/src/test/njs_unit_test.c
+++ b/src/test/njs_unit_test.c
@@ -14431,6 +14431,9 @@
     { njs_str("var d = new Date(2011, 5, 24, 18, 45); d.toDateString()"),
       njs_str("Fri Jun 24 2011") },
 
+    { njs_str("new Date(NaN).toDateString()"),
+      njs_str("Invalid Date") },
+
     { njs_str("var d = new Date(2011, 5, 24, 18, 45);"
                  "d.toTimeString().slice(0, 17)"),
       njs_str("18:45:00 GMT+0000") },
@@ -14477,6 +14480,9 @@
     { njs_str("(new Date('-012345-07-01T00:00Z')).toUTCString()"),
       njs_str("Thu, 01 Jul -12345 00:00:00 GMT") },
 
+    { njs_str("new Date(NaN).toUTCString()"),
+      njs_str("Invalid Date") },
+
     { njs_str("var d = new Date(-62167219200000); d.toISOString()"),
       njs_str("0000-01-01T00:00:00.000Z") },
 
@@ -14485,6 +14491,9 @@
 
     { njs_str("var d = new Date(-62198755200000); d.toISOString()"),
       njs_str("-000001-01-01T00:00:00.000Z") },
+
+    { njs_str("new Date(NaN).toISOString()"),
+      njs_str("RangeError: Invalid time value") },
 #endif
 
     { njs_str("Date.UTC(2011, 5, 24, 6, 0)"),