Jump to content

Recommended Posts

I'm trying to add 180 days to an existing date, and then make sure if the month is a single digit, I want to add a 0 before it.

For some reason its just not working, I am hoping someone can point out my error... thanks!

 

	var dateStr = "08/22/2001";
	var millis = Date.parse(dateStr);
	var newDate = new Date();
	newDate.setTime(millis  + 180 *24*60*60*1000);
	var newDateStr = "" + (newDate.getMonth()+1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
	myarray = newDateStr.split(/\//);
	if( myarray[0] <= 9 && myarray[0] >= 0)
	{ 
		var save = "/" + myarray[0] + "/";
		alert(myarray[0]);
		newmonth = "0"+myarray[0]+"/";
		alert(newmonth );
		myregexp = save+"/";
		alert(myregexp);
		newDateStr2 = newDateStr.replace(myregexp, newmonth );
		alert(newDateStr2);

	}

Link to comment
https://forums.phpfreaks.com/topic/101806-my-regex-isnt-working/
Share on other sites

I'm still not quite sure what's wrong with your original code, but it would be much simpler (and easier) to use something like the following (note this also makes sure that the day is two digits):

 

var dateStr = "08/22/2001";
var millis = Date.parse(dateStr);
var newDate = new Date();
newDate.setTime(millis  + 180*24*60*60*1000);
var newDateStr = "";
if (newDate.getMonth() < 9)
newDateStr += "0";
newDateStr += (newDate.getMonth()+1)+"\/";
if (newDate.getDate() < 10)
newDateStr += "0";
newDateStr += newDate.getDate()+"\/"+newDate.getFullYear();
alert(newDateStr);

 

I could probably do this in several fewer lines using a one line regular expression, but my brain can't process regEx this late at night. I think this is a little more elegant.

 

MORE IMPORTANTLY: The code in the previous two posts will not calculate correctly for abourt 1/2 the dates (8/22/2001 being one of them due to daylight savings going back for the fall equinox). because the date you used to create the original timestamp has no time it is assumed to be midnight. With 8/22/2001 if you add 24 hours x 180 days it will only take you to 10PM on the 179th day. I added four hours as a buffer to account for that.

 

  var dateStr = "08/22/2001";
  var millis = Date.parse(dateStr);
  var newDate = new Date();
  //Create new date object +180 days &
  // +4 hours so daylight savings won't mess it up
  newDate.setTime(millis+(180*24*60*60*1000)+(4*60*60*1000));

  //Create string objects of month & day with 0 appended
  //Then take jsut the last two characters
  var month = String('0'+(newDate.getMonth()+1));
      month = month.substr((month.length-2), 2);
  var day   = String('0'+newDate.getDate());
      day   = day.substr((day.length-2), 2);
  var year  = String(newDate.getFullYear());

  //Create new date string
  var newDateStr = month+'/'+day+'/'+year;

  alert(newDateStr);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.