Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-15-2012, 01:21 PM   PM User | #1
mattps
New to the CF scene

 
Join Date: Aug 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
mattps is an unknown quantity at this point
Parsing to a date object

Hello,

I am trying to pass a variable to a date object and then increment the date object. The format of the variable is dd/mm/yyyy. I have tried using Date.parse but am getting incorrect results:

The date I am trying parse is:

Quote:
15/10/2012
Code:
var myDatetemp=Date.parse(document.getElementById("loggeddate").value);
var myDate=new Date(myDatetemp);
I then increment the date by, say 1 day:

Code:
myDate.setDate(myDate.getDate()+1);document.getElementById('deadline').value=myDate.getDate() + "/" + (myDate.getMonth()+1)+ "/" + myDate.getYear();break;
What I get returned is:

Quote:
11/3/2013
mattps is offline   Reply With Quote
Old 10-15-2012, 01:34 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
The problem is that a string date will generally be interpreted as mm/dd/yyyy. And the 10th day of the 15th month 2012 will be "translated" into the 10th day of the 3rd month 2013. And +1 will be the 11th day then.

Try this (make sure that day and month are always two digits!)
Code:
var mydatestring = document.getElementById("loggeddate").value;
var myyear = parseInt(mydatestring.substring(6), 10);
var mymonth = parseInt(mydatestring.substring(3, 5), 10) - 1;
var myday = parseInt(mydatestring.substring(0, 2), 10);
var myDate = new Date(myyear, mymonth, myday);
myDate.setDate(myDate.getDate()+1);
document.getElementById('deadline').value=myDate.getDate() + "/" + (myDate.getMonth()+1)+ "/" + myDate.getYear();
devnull69 is offline   Reply With Quote
Old 10-15-2012, 02:03 PM   PM User | #3
shyagrawal
New Coder

 
Join Date: Sep 2012
Posts: 22
Thanks: 0
Thanked 6 Times in 6 Posts
shyagrawal is an unknown quantity at this point
Try using this:

var Date = new Date();
var DaysToAdd = 6;
someDate.setDate(Date.getDate() + DaysToAdd);

Formatting Date to dd/mm/yyyy format:
var dd = Date.getDate();
var mm = Date.getMonth() + 1;
var yyyy = Date.getFullYear();

var NewDate = dd + '/'+ mm + '/'+ yyyy;

Hope this helps.
shyagrawal is offline   Reply With Quote
Old 10-15-2012, 02:43 PM   PM User | #4
mattps
New to the CF scene

 
Join Date: Aug 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
mattps is an unknown quantity at this point
Thank you both for your help.
mattps is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:11 PM.


Advertisement
Log in to turn off these ads.