0

I'm working with dates using javascript, and have come across a odd problem

                function updateRow(element){
                    var data = element.name.match(/stamp\[(\d+)\]\[(\d+)\]/);

                    if(data[2] == 3){
                        var raw = element.value.match(/(\d+):(\d+):(\d+)/);
                        var time = 0;//((raw[1] * 3600) + (raw[2] * 60) + (raw[3] * 1)) * 1000;

                        //Hardcoded just to test
                        var test = new Date("2015-02-18 13:16:06");
                        var date = new Date(test.getTime());

                        document.getElementById(data[1]+"-2").value = 
                                date.getFullYear()+"-"+

                                fillZero(date.getMonth(), 2)+"-"+
                                fillZero(date.getDay(),   2)+" "+


                                fillZero(date.getHours(),  2)+":"+
                                fillZero(date.getMinutes(),2)+":"+
                                fillZero(date.getSeconds(),2);
                    }
                }

The date I get from should be the 2015-02-18 13:16:06, but from some odd reason I get 2015-01-03 13:16:06, but I have no idea why.

2
  • 2
    month's in javascript start from 0. 0 - jan, 1 - feb Commented Feb 26, 2015 at 10:50
  • But the day changes to, 20 becomes 3 Commented Feb 26, 2015 at 10:54

1 Answer 1

2

Use date.getDate() instead of date.getDay() and add +1 in getMonth

Sign up to request clarification or add additional context in comments.

1 Comment

I will as soon as it will let me.

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.