英文:
Add one month to current Unix timestamp in javascript
问题
我要将这个时间戳 1688452200
增加一个月,但这不起作用。
const startdate = new Date('1688452200');
const newstartDate = new Date(startdate.setMonth(startdate.getMonth() + 1));
console.log('newDate:', newstartDate);
const newStartDateTime = newstartDate.getTime();
英文:
I have this timestamp 1688452200
const startdate = new Date('1688452200')
const newstartDate = new Date(startdate.setMonth(startdate.getMonth() + 1));
console.log('newDate:', newstartDate);
const newStartDateTime = newstartDate.getTime();
want to add 1 month to this timestamp but this is not working
答案1
得分: 2
需要做一些事情:
- UNIX时间戳位于UTC,因此我们需要添加时区偏移量
- 将结果的UNIX时间戳转换为JS时间戳(乘以毫秒)。
const startdate = new Date((unixTimestamp - new Date().getTimezoneOffset() * 60) * 1000);
console.log('startdate:', startdate);
startdate.setMonth(startdate.getMonth() + 1);
console.log('newDate:', startdate);
<script>
const unixTimestamp = (new Date(2023, 4, 31).getTime() / 1000).toString();
</script>
英文:
You need a few things here:
- a UNIX timestamp is in the UTC so we need to add the timezone offset to it
- convert the resulting UNIX timestamp to the JS timestamp (multiply by milliseconds).
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const startdate = new Date((unixTimestamp - new Date().getTimezoneOffset() * 60) * 1000);
console.log('startdate:', startdate);
startdate.setMonth(startdate.getMonth() + 1);
console.log('newDate:', startdate);
<!-- language: lang-html -->
<script>
const unixTimestamp = (new Date(2023,4,31).getTime() / 1000).toString();
</script>
<!-- end snippet -->
答案2
得分: 0
const oneMonthToUnixTimestamp = () => {
let date = new Date();
const month = date.getMonth() + 1;
let year = date.getYear();
const epoch = date.valueOf();
let dayToHr = 24;
const hrToMin = 60;
const minToSec = 60;
const secToMill = 1000;
let numberofdate = new Date(year, month, 0).getDate();
let addedmillisec = numberofdate * dayToHr * hrToMin * minToSec * secToMill;
return epoch + addedmillisec
}
console.log(oneMonthToUnixTimestamp())
英文:
const oneMonthToUnixTimestamp = () => {
let date = new Date();
const month = date.getMonth() + 1;
let year = date.getYear();
const epoch = date.valueOf();
let dayToHr = 24;
const hrToMin = 60;
const minToSec = 60;
const secToMill = 1000;
let numberofdate = new Date(year, month, 0).getDate();
let addedmillisec = numberofdate * dayToHr * hrToMin * minToSec * secToMill;
return epoch + addedmillisec
}
console.log(oneMonthToUnixTimestamp())
答案3
得分: -1
const startdate = new Date(1688452200*1000);
const newstartDate = new Date(startdate.setMonth(startdate.getMonth() + 1));
console.log('newDate:', newstartDate);
const newStartDateTime = newstartDate.getTime();
英文:
const startdate = new Date(1688452200*1000);
const newstartDate = new Date(startdate.setMonth(startdate.getMonth() + 1));
console.log('newDate:', newstartDate);
const newStartDateTime = newstartDate.getTime();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论