support date "now"

This commit is contained in:
2023-02-20 18:46:52 +10:00
parent 83f6bce923
commit b50acd3b74

View File

@@ -1,3 +1,5 @@
import { transformWithEsbuild } from "vite";
export class SMDate { export class SMDate {
date: Date | null = null; date: Date | null = null;
dayString: string[] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; dayString: string[] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
@@ -46,7 +48,7 @@ export class SMDate {
dateOrString: string | Date = "", dateOrString: string | Date = "",
options: { format?: string; utc?: boolean } = {} options: { format?: string; utc?: boolean } = {}
) { ) {
this.date = new Date(); this.date = null;
if (typeof dateOrString === "string") { if (typeof dateOrString === "string") {
if (dateOrString.length > 0) { if (dateOrString.length > 0) {
@@ -75,6 +77,11 @@ export class SMDate {
): SMDate { ): SMDate {
const now = new Date(); const now = new Date();
if (dateString.toLowerCase() == "now") {
this.date = now;
return this;
}
// Parse the date format to determine the order of the date components // Parse the date format to determine the order of the date components
const order = (options.format || "dmy").toLowerCase().split(""); const order = (options.format || "dmy").toLowerCase().split("");
options.utc = options.utc || false; options.utc = options.utc || false;
@@ -441,3 +448,5 @@ export class SMDate {
return result; return result;
} }
} }
console.log(new SMDate("").isValid());