Add one month to current Unix timestamp in javascript

huangapple go评论72阅读模式
英文:

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(&#39;startdate:&#39;, startdate);
startdate.setMonth(startdate.getMonth() + 1);
console.log(&#39;newDate:&#39;, startdate);

<!-- language: lang-html -->

&lt;script&gt;
  const unixTimestamp = (new Date(2023,4,31).getTime() / 1000).toString();
&lt;/script&gt;

<!-- 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 = () =&gt; {  
    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(&#39;newDate:&#39;, newstartDate);
const newStartDateTime = newstartDate.getTime();

huangapple
  • 本文由 发表于 2023年6月26日 18:23:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定