Search code examples
javascriptarraysunixdatetimeutc

Pick a date then convert format from UNIX to UTC in yyyymmdd


Objective: Setup the date in the array at the end of the block to read: yyyymmdd including the zeros. (example data build: numDaysAPITimes = [20150403]). What I got is not getting the month correctly and the numDaysAPITimes array is storing only the year for some reason.

    var totalPrecipSinceDate;
        var numDaysAPITimes = [];
        var userDataDatePick =  document.getElementById('dateRngPick').value; 

     if (userDataDatePick >=1)
        {
        for (var i = 0; i <= (userDataDatePick-1); i++) //place user userData-1 where i <= input
            {
                var myDate = new Date(); //http://stackoverflow.com/questions/7693170/javascript-convert-from-epoch-string-to-date-object
                var epoch = myDate.getTime(); 
                var unixEpoch = Math.round(epoch/1000)
                var backDateEpochTime = Math.round(unixEpoch - (86400 * i)); //Get each day (UNIX seconds)
                  var d = new Date(backDateEpochTime); //Convert to UTC
                var curr_date = d.getDate();
                var curr_month = d.getMonth() + 1; //Months are zero based
                var curr_year = d.getFullYear();

                numDaysAPITimes[i] = (curr_year + curr_month + curr_date);
            }
        }
    else
        {
           alert("You have not entered a valid number for the date.");

           numDaysAPITimes.length = 0;
        }

Solution

  • a couple things:

    1. your date info is getting added together as numbers, that why it seems the year is only going through. One way to handle that would be to use the toString() method.

    2. you'll probably want leading zeroes on the day and month, which you can achieve by prepending a 0 and then doing a slice -2.

    That would looks like This JSFiddle, or:

        var totalPrecipSinceDate;
            var numDaysAPITimes = [];
            var userDataDatePick =  2;//document.getElementById('dateRngPick').value; 
    
         if (userDataDatePick >=1)
            {
            for (var i = 0; i <= (userDataDatePick-1); i++) //place user userData-1 where i <= input
                {
                    var myDate = new Date(); //http://stackoverflow.com/questions/7693170/javascript-convert-from-epoch-string-to-date-object
                    var epoch = myDate.getTime(); 
                    var unixEpoch = Math.round(epoch/1000)
                    var backDateEpochTime = Math.round(unixEpoch - (86400 * i)); //Get each day (UNIX seconds)
                      var d = new Date((1000*backDateEpochTime)); //Convert to UTC
                    var curr_date = ("0" + d.getDate()).slice(-2)
                    var curr_month = ("0"+ (d.getMonth() + 1)).slice(-2); //Months are zero based
                    var curr_year = d.getFullYear();
                    console.log(d.getMonth());
                    numDaysAPITimes[i] = (curr_year.toString() + curr_month.toString() + curr_date.toString());
                }
            }
        else
            {
               alert("You have not entered a valid number for the date.");
    
               numDaysAPITimes.length = 0;
            }
    console.log(numDaysAPITimes)