Reputation: 792
I don't seem to be able to get my head around the syntax required to have two date variables.
var videodate = new Date(2014,7,5);
var previousdate = videodate.getDate()-1;
This would allow me to have two links on the page:
<a href="today.mp4">{videodate}</a>
<a href="yesterday.mp4">{previousdate}</a>
What I am finding is that the solutions I've hunted around for use:
videodate.setDate(videodate.getDate()-1)
but that then changes the 'videodate' variable. I just want to be able to read 'videodate' in order to specify the 'previousdate'!? So something like:
var previousdate = new Date(videodate.getDate()-1)
Upvotes: 1
Views: 41
Reputation: 15464
Convert into timestamp
var videodatets = new Date('July 07 2014').getTime();
var previousdatets = videodatets-24*60*60*1000;
var videodate=new Date(videodatets);
var previousdate=new Date(previousdatets);
alert(videodate);
alert(previousdate);
Upvotes: 0
Reputation: 30993
You can substract the time in millisecond of a day a build a new date object:
var videodate = new Date(2014,7,5);
var previousdate = new Date(videodate.getTime() - (24 * 60 * 60 * 1000));
or alternatively use moment.js
, it will make you life easier on date stuff:
var videodate = new Date(2014,7,5);
var previousdate = moment(videodate).subtract('days', 1);
In both ways the original videodate
variable will not be touched.
Demo: http://jsfiddle.net/IrvinDominin/pcPCJ/
Upvotes: 1
Reputation: 64526
Create a separate new date object for the previous date, setting it to the same date as the first by using .getTime()
, then do the -1 on it, keeping the two dates independent of one another.
var videodate = new Date(2014,7,5);
var previousdate = new Date(videodate.getTime());
previousdate.setDate(previousdate.getDate()-1);
Upvotes: 3