admin管理员组文章数量:1025217
My project has a date picker and when user selects a date I'm getting it to unix time stamp and it look like following.
1517769000
I'm getting that like this(This is a RN project)
var selectedDate = moment(this.state.date).unix();
Now I need to get only the year from above date in JS. I tried selectedDate.year() as well. But it's always getting 1970. Please help me to solve this.
My project has a date picker and when user selects a date I'm getting it to unix time stamp and it look like following.
1517769000
I'm getting that like this(This is a RN project)
var selectedDate = moment(this.state.date).unix();
Now I need to get only the year from above date in JS. I tried selectedDate.year() as well. But it's always getting 1970. Please help me to solve this.
Share Improve this question edited Feb 22, 2018 at 18:26 apple_92 asked Feb 22, 2018 at 18:20 apple_92apple_92 2632 gold badges7 silver badges18 bronze badges 2- Given it’s a moment object you should just be able to do moment(selectedDate).year() and it will give you the year. Refer to member.js docs for formatting options. Updated accordingly. – SwankTheTank Commented Feb 22, 2018 at 18:24
- 1 See what is collected as "Related" on the right. A mere search could have worked too... – tevemadar Commented Feb 22, 2018 at 18:25
5 Answers
Reset to default 3This should do the trick
let date = new Date(1517769000 * 1000).getFullYear()
console.log(date)
EDIT
added multiplication by 1000 as Unix timestamp is in seconds and JavaScript Date
is in msecs
Here you go..
new Date(selectedDate).getFullYear();
simply multiply the timestamp with 1000 -> ms.
date = new Date(1517769000 * 1000).getFullYear()
Unix timestamps do not include microseconds, whereas Javascript dates do.
new Date(selectedDate * 1000).getFullYear();
will yield the correct value when you feed it a Unix timestamp.
The other answers are correct if the timestamp came from JavaScript in the first place.
Unix time (<-that is a link, just not very visible)
number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970 [...]
new Date() (<- also a link)
Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC [...]
Multiply with 1000, then it will bee better:
console.log(new Date(1517769000*1000).getFullYear());
My project has a date picker and when user selects a date I'm getting it to unix time stamp and it look like following.
1517769000
I'm getting that like this(This is a RN project)
var selectedDate = moment(this.state.date).unix();
Now I need to get only the year from above date in JS. I tried selectedDate.year() as well. But it's always getting 1970. Please help me to solve this.
My project has a date picker and when user selects a date I'm getting it to unix time stamp and it look like following.
1517769000
I'm getting that like this(This is a RN project)
var selectedDate = moment(this.state.date).unix();
Now I need to get only the year from above date in JS. I tried selectedDate.year() as well. But it's always getting 1970. Please help me to solve this.
Share Improve this question edited Feb 22, 2018 at 18:26 apple_92 asked Feb 22, 2018 at 18:20 apple_92apple_92 2632 gold badges7 silver badges18 bronze badges 2- Given it’s a moment object you should just be able to do moment(selectedDate).year() and it will give you the year. Refer to member.js docs for formatting options. Updated accordingly. – SwankTheTank Commented Feb 22, 2018 at 18:24
- 1 See what is collected as "Related" on the right. A mere search could have worked too... – tevemadar Commented Feb 22, 2018 at 18:25
5 Answers
Reset to default 3This should do the trick
let date = new Date(1517769000 * 1000).getFullYear()
console.log(date)
EDIT
added multiplication by 1000 as Unix timestamp is in seconds and JavaScript Date
is in msecs
Here you go..
new Date(selectedDate).getFullYear();
simply multiply the timestamp with 1000 -> ms.
date = new Date(1517769000 * 1000).getFullYear()
Unix timestamps do not include microseconds, whereas Javascript dates do.
new Date(selectedDate * 1000).getFullYear();
will yield the correct value when you feed it a Unix timestamp.
The other answers are correct if the timestamp came from JavaScript in the first place.
Unix time (<-that is a link, just not very visible)
number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970 [...]
new Date() (<- also a link)
Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC [...]
Multiply with 1000, then it will bee better:
console.log(new Date(1517769000*1000).getFullYear());
本文标签: javascriptHow to get only the year from unix time stampStack Overflow
版权声明:本文标题:javascript - How to get only the year from unix time stamp? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745614149a2159183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论