使用 moment 我们如何更改或设置日期并保持 unix 时间格式

using moment how do we change or set date and keep unix time format

我们可以使用moment更改或设置日期并保持unix时间格式吗?

当前时间格式为 1629442800,在人眼中是 2021 年 8 月 20 日 3:00:00 上午。
我只想更改日期但保持时间不变

current time format is 1629442800 which in human eyes August 20, 2021 3:00:00 AM
desired time 1630047600 which in human eyes  August 27, 2021 3:00:00 AM
//using javascript  not sure how to use this
const currentTime = 1629442800

moment(currentTime).set('year', 2013);
moment(currentTime).set('month', 3);  // April
moment(currentTime).set('date', 1);

console.log(currentTime)

每次调用 moment(currentTime) 都会创建多个不同的日期对象,而 currentTime 将仅用于初始化该数据。 currentTime 不会受到任何 .set 调用的影响。

所以这部分代码完全没用:

moment(currentTime).set('year', 2013);
moment(currentTime).set('month', 3);  // April
moment(currentTime).set('date', 1);

您只需创建时刻日期对象,对其调用 .set,然后丢弃结果。

必须是:

const currentTime = 1629442800

// create a moment time object
const timeObject = moment.unix(currentTime)

// do changes on that object
timeObject.set('year', 2013);
timeObject.set('month', 3);  // April
timeObject.set('date', 1);

// get the unix timestamp form that moment object
console.log(timeObject.unix())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>