I have to understand whether the given string is in YYYY-MM-DD
format.
I was tetsting using the following code snippet:
const isValidDate = (new Date(timestamp)).getTime() > 0;
However, in this case, "2015-06-22T13:17:21+0000" will be also valid.
I want to ensure only the day part is present in YYYY-MM-DD
.
What would be a neat concise way to determine this in javascript.
Well you could do a regex test on the input string:
var input = "2015-06-22"; if (/^\d{4}-\d{2}-\d{2}$/.test(input)) { console.log("MATCH"); }
If your requirement really be that the timestamp be precisely at midnight, you could also check the hours, minutes, and seconds components:
var date = new Date(); date.setHours(0, 0, 0, 0); if (date.getHours() == 0 && date.getMinutes() == 0 && date.getSeconds() == 0) { console.log("Date is at midnight"); }
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.