asn1-ts
Version:
ASN.1 encoding and decoding, including BER, CER, and DER.
15 lines (14 loc) • 779 B
JavaScript
import convertTextToBytes from "../../../utils/convertTextToBytes.mjs";
import * as errors from "../../../errors.mjs";
export default function encodeDateTime(value) {
if (value.getFullYear() < 1582 || value.getFullYear() > 9999) {
throw new errors.ASN1Error(`The DATE ${value.toISOString()} may not be encoded, because the `
+ "year must be greater than 1581 and less than 10000.");
}
return convertTextToBytes(value.getFullYear().toString().padStart(4, "0")
+ (value.getMonth() + 1).toString().padStart(2, "0")
+ value.getDate().toString().padStart(2, "0")
+ value.getHours().toString().padStart(2, "0")
+ value.getMinutes().toString().padStart(2, "0")
+ value.getSeconds().toString().padStart(2, "0"));
}