我正在尝试比较字符串格式的法国日期,如Vendredi 22 Mai à 22h (字面意思是时间5月22日星期五晚上10点)与使用node.js的javascript中的当前日期:
const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss'); // or maybe with new Date.now()
const dateToCompare = "Vendredi 22 Mai à 22h"
if (dateNow <= dateToCompare) {
// Code here
}感谢您的帮助!
发布于 2020-05-23 04:56:39
您需要找到一种解析法国日期的方法,因为浏览器在这方面没有很好的一致性。一旦它被解析,你就可以通过转换成它的“原始值”来检查哪个日期是最近的。
const currentYear = Date.now().getFullYear(); // since this isn't in your date string.
if (Date.now() <= new Date(currentYear, 5 , 22, 22).valueOf()) {
// code
}另请参阅:
日期对象文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Moment docs (一个流行的日期库):https://momentjs.com/
发布于 2020-06-03 10:10:31
我让一些人这样想,如果它能帮助其他人(考虑到我的JS技能,肯定不是最好的代码):
const dateFormat = require('dateformat');
const monthsArr = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'];
const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss');
const currentYear = dateFormat(new Date(), 'yyyy');
// Split each word
let eventDate = textDescription.split(' ');
// Organize the array to save only the necessary values : day in number, month in number and hours:minutes
eventDate = [
eventDate[3],
monthsArr.indexOf(eventDate[4]),
eventDate[6]
];
// Split hours:minutes
// Modify hours number in date array
// Push minutes number to the date array
const eventHourMin = eventDate[2].split('h');
eventDate[2] = eventHourMin[0];
eventDate.push(eventHourMin[1]);
// Full date converted as yyyy-mm-dd HH:MM:ss
const formattedEventDate = dateFormat(new Date(currentYear, eventDate[1], eventDate[0], eventDate[2], eventDate[3], '00'), 'yyyy-mm-dd HH:MM:ss');
// If event date is not expired
if (dateNow < formattedEventDate) {
// code here...
}https://stackoverflow.com/questions/61963443
复制相似问题