最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to get only the year from unix time stamp? - Stack Overflow

matteradmin9PV0评论

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
Add a ment  | 

5 Answers 5

Reset to default 3

This 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());

Post a comment

comment list (0)

  1. No comments so far