Add one month to current Unix timestamp in javascript

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

Add one month to current Unix timestamp in javascript

问题

我要将这个时间戳 1688452200 增加一个月,但这不起作用。

  1. const startdate = new Date('1688452200');
  2. const newstartDate = new Date(startdate.setMonth(startdate.getMonth() + 1));
  3. console.log('newDate:', newstartDate);
  4. const newStartDateTime = newstartDate.getTime();
英文:

I have this timestamp 1688452200

  1. const startdate = new Date('1688452200')
  2. const newstartDate = new Date(startdate.setMonth(startdate.getMonth() + 1));
  3. console.log('newDate:', newstartDate);
  4. const newStartDateTime = newstartDate.getTime();

want to add 1 month to this timestamp but this is not working

答案1

得分: 2

需要做一些事情:

  • UNIX时间戳位于UTC,因此我们需要添加时区偏移量
  • 将结果的UNIX时间戳转换为JS时间戳(乘以毫秒)。
  1. const startdate = new Date((unixTimestamp - new Date().getTimezoneOffset() * 60) * 1000);
  2. console.log('startdate:', startdate);
  3. startdate.setMonth(startdate.getMonth() + 1);
  4. console.log('newDate:', startdate);
  1. <script>
  2. const unixTimestamp = (new Date(2023, 4, 31).getTime() / 1000).toString();
  3. </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 -->

  1. const startdate = new Date((unixTimestamp - new Date().getTimezoneOffset() * 60) * 1000);
  2. console.log(&#39;startdate:&#39;, startdate);
  3. startdate.setMonth(startdate.getMonth() + 1);
  4. console.log(&#39;newDate:&#39;, startdate);

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

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

<!-- end snippet -->

答案2

得分: 0

  1. const oneMonthToUnixTimestamp = () => {
  2. let date = new Date();
  3. const month = date.getMonth() + 1;
  4. let year = date.getYear();
  5. const epoch = date.valueOf();
  6. let dayToHr = 24;
  7. const hrToMin = 60;
  8. const minToSec = 60;
  9. const secToMill = 1000;
  10. let numberofdate = new Date(year, month, 0).getDate();
  11. let addedmillisec = numberofdate * dayToHr * hrToMin * minToSec * secToMill;
  12. return epoch + addedmillisec
  13. }
  14. console.log(oneMonthToUnixTimestamp())
英文:
  1. const oneMonthToUnixTimestamp = () =&gt; {
  2. let date = new Date();
  3. const month = date.getMonth() + 1;
  4. let year = date.getYear();
  5. const epoch = date.valueOf();
  6. let dayToHr = 24;
  7. const hrToMin = 60;
  8. const minToSec = 60;
  9. const secToMill = 1000;
  10. let numberofdate = new Date(year, month, 0).getDate();
  11. let addedmillisec = numberofdate * dayToHr * hrToMin * minToSec * secToMill;
  12. return epoch + addedmillisec
  13. }
  14. 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();

英文:
  1. const startdate = new Date(1688452200*1000);
  2. const newstartDate = new Date(startdate.setMonth(startdate.getMonth() + 1));
  3. console.log(&#39;newDate:&#39;, newstartDate);
  4. 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:

确定