简体   繁体   中英

Compare SQL datetime to current date and time in TS

I have a function that takes an array of type any[] and compares the ExpiryDate property with the current date and time. If the ExpiryDate property is bigger then the current date and time, then it's a valid passport in this case, otherwise the passport has expired

currentPassports: any[];
oldPassports: any[];
passports: any[];

processPassports(passports: any[]): void {
    var arrayLength = passports.length;
        for (var i = 0; i < arrayLength; i++) {
            if (passports[i].ExpiryDate > CurrentDateTime)
        // Add the object to currentPassports
            else
        // Add the object to expiredPassports 
    }
}

What is the best way to compare SQL datetime to the current time in TypeScript? And how can I EXACTLY compare each object's ExpiryDate (also include Milliseconds in the comparison)

Note: CurrentDateTime is just a placeholder

You don't mention if the SQL datetime coming in is already a date or if it is a string. If you do need to parse the SQL date, I have found Moment.js to be one of the easiest libraries to use.

If you need to get the current date and time in javascript just call, new Date()

If you need to compare the two dates, including milliseconds, follow this: https://stackoverflow.com/a/327458/5656858

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM