请帮助从给定日期获得正确的月份。我花了很多时间,几乎卡住了..
这是一个函数:
所以任务是检查月份和年份是否等于给定日期。如果 dateLeft 为空,则在 1970-01-01 和给定日期之间进行比较。
我正在使用 date-fns 中的 getMonth 函数来获取月份
import { parseISO, format, getMonth, getYear } from 'date-fns'
import ru from 'date-fns/locale/ru'
const ifMonthAndYearIsEqual = (dateLeft, dateRight) => {
const date1 = dateLeft ? new Date(dateLeft.seconds * 1000) : new Date(1970, 1, 1)
const date2 = new Date(dateRight.seconds * 1000)
console.log("TCL: ifMonthAndYearIsEqual -> dateLeft", dateLeft)
console.log("TCL: ifMonthAndYearIsEqual -> dateRight", dateRight)
console.log("TCL: ifMonthAndYearIsEqual -> date1", date1)
console.log("TCL: ifMonthAndYearIsEqual -> date2", date2)
console.log("TCL: ifMonthAndYearIsEqual -> getMonth(date1)", getMonth(date1))
console.log("TCL: ifMonthAndYearIsEqual -> getMonth(date2)", getMonth(date2))
const monthIsEqual = (getMonth(date1) === getMonth(date2))
console.log(monthIsEqual)
}
export default ifMonthAndYearIsEqual
date.Right我来自 firebase。这是一个时间戳对象,具有秒和纳秒。
在我的情况下,我总是有console.log(getMonth(date2))
0,但给定日期是1579254008以秒为单位。怎么了?