Issue
I want to save unixtime to a state of type Moment.moment.
If you use moment(timestamp), you will get a different date.
const [date, setDate] = useState<moment.Moment | null>(null);
const timestamp = Math.floor(date.getTime() / 1000);
setDate(moment(timestamp));
Solution
You manipulated the date when you used Math.floor.
console.log(moment());
console.log('With Floor', moment.unix( Math.floor((new Date()).getTime() /1000 )));
console.log('Without Floor', moment.unix((new Date()).getTime() /1000 ));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Good Read: You Don't Need MomentJS
Answered By - Someone Special
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.