0

I currently am using this to create a Unix time stamp for time (now) -1 year's time.

Can someone please share a better and more efficient way to do this?

var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var lastYear = parseInt(currentYear) - 1;
var lastYearDateObj = new Date(lastYear, currentDate.getMonth(), currentDate.getDate(), currentDate.getHours(), currentDate.getMinutes());
var lastYearTime = lastYearDateObj.getTime() / 1000;

Thank you!

1

1 Answer 1

2

I don't understand what your division by 1000 is about. You can add it to the end if you like:

var date = new Date();
date.setFullYear(date.getFullYear() - 1);
// date.getTime() / 1000 // if you want.
Sign up to request clarification or add additional context in comments.

4 Comments

getTime returns milliseconds, so dividing by 1000 gives seconds (unix time in seconds).
I've always used timestamps in milliseconds (or microseconds in some systems), not just seconds. That's why I didn't understand. Is there some Unix timestamp in seconds?
"Unix time, or POSIX time, is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,[note 1] not counting leap seconds." en.wikipedia.org/wiki/Unix_time
Thanks. Just looking at that. Strange that I've always used milli- or micro-second timestamps from other systems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.