1

Here is my solution to this: Thank you for all of the suggestions and help. I ended up using moment.js and now this date situation is working as I needed. Here is what I did with moment.js:

var preorderdate = moment(date).subtract('days',10).format('MMMM D, YYYY');
var releasedate = moment(date).format('MMMM D, YYYY');

I renamed some of the variables but you can see that I took the date variable, subtracted 10 days and formatted it to February 12, 2014. I took a separate variable and just formatted it with moment.js. This script is pretty awesome. I never, ever would've found it without Pointy's suggestion. Thank you all so much!

I'm not a JS expert but I'm trying to subtract 10 days from a string of 'February 12, 2014' to no avail. So when I run my code, I'm trying to get it to display as 'February 2, 2014.'

function isbnPreOrder (isbn, date) {
    var date = 'February 12, 2014';
    var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var d = new Date();
    var curr_date = d.getDate()-10;
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    var predate = (m_names[curr_month]+" "+curr_date+", "+curr_year);
}
thedonlee
  • 25
  • 3
  • I just tried adding the following but it just returns this value 1391797140943: var dateOffset = (24*60*60*1000) * 10; var predate= (d.setTime(d.getTime() - dateOffset)); – thedonlee Feb 12 '14 at 18:19
  • Duplicate of http://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript – Tom Feb 12 '14 at 18:20

4 Answers4

2

Make a Date instance:

var date = new Date("2014-02-12");

Then set its timestamp back by 10 days:

date.setDate( date.getDate() - 10 );

You cannot rely on browsers being able/willing to parse the date format "February 12, 2014". If you have no choice at all in the date format, then you'll have to parse that yourself or use a helper library. (Another possibility here.)

Pointy
  • 405,095
  • 59
  • 585
  • 614
2

I'd modify your function to:

function isbnPreOrder (isbn, date) {
    var date = 'February 12, 2014';
    var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    var predate = (m_names[curr_month]+" "+curr_date+", "+curr_year);
    var x = 10;
    predate.setDate(predate.getDate() - x);
}
0

try this:

function isbnPreOrder (isbn, date) {
var date = 'February 12, 2014';
var m_names = new Array("January", "February", "March", "April", "May", "June", "July",   "August", "September", "October", "November", "December");
var d = new Date();
var curr_date = d.setDate(d.getDate()-10);
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var predate = (m_names[curr_month]+" "+curr_date+", "+curr_year);
ghosty89
  • 86
  • 4
0

all is right except for one line. pass the date variable into your Date initialization.

var d = new Date(date);
michael truong
  • 321
  • 1
  • 4