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