Date format

edited December 2017 in p5.js Programming Questions

Is SimpleDateFormat compatible with P5.JS? I need to convert "YYYY-MM-DD HH:mm:ss" to just a simple "HH". I can't seam to find any instruction on how to set it up. Thanks.

Answers

  • if it's a fixed input format then you can just use a substring.

  • Answer ✓

    SimpleDateFormat is a Java Class; so the nearest equivalent for use with p5js is JavaScript's built-in Date object...

    ...though as koogs says; depending on your needs a substring might be simpler.

  • Thanks for the link. I can make this work

  • The link was fantastic. I was able to get it to work. here is what I wrote:

    ii = -16;//start Time lable section
    DateTime = avg.get(forecast, 1);//avg is my file.csv and forecast is the row number of interest
    front = DateTime.substr(0, 10);//2016-05-25 15:00:00, silly time is missing the 'T'
    back = DateTime.substr(11);
    stringDateTime = front + "T" + back;// insert T, //2016-05-25T15:00:00
    newShadeTime = new Date(stringDateTime);
    for (i = 48; i < 970; i = i + 40) { //date draw
        front = DateTime.substr(0, 10);//2016-05-25 15:00:00
        back = DateTime.substr(11);
        stringDateTime = front + "T" + back;// insert T, //2016-05-25T15:00:00
        newDateTime = new Date(stringDateTime);
        TimeZoneOffset = (newDateTime.getTimezoneOffset() / 60);
        dateHour = newDateTime.getHours();
        TimeZoneOffset = dateHour - TimeZoneOffset;
        newDateTime.setHours((TimeZoneOffset));
        ii = ii + 8;
        var checkTime = newDateTime.getHours();
        var hourStep = checkTime + ii;
        newDateTime.setHours(hourStep);
        dateHour = newDateTime.getHours();
        dateMonth = newDateTime.getMonth();
        dateDate = newDateTime.getDate();
        dateDay = newDateTime.getDay();
    
        push(); // hour
        translate(77, 470 + graphNumber);
        rotate(-HALF_PI);
        fill(220);
        textSize(16);
        noStroke();
        textAlign(RIGHT);
        text(dateHour + ":", 0, i);// hours
        if (ii == 0 || ii == 24 || ii == 48 || ii == 72 || ii == 96 || ii == 120 || ii == 144 || ii == 168) {
            text(MonName[dateMonth] + " " + dateDate, -30, i - 1);// date
            rotate(HALF_PI);
            text(DayNames[dateDay], i + 50, 45);
        }
        pop();
    
        if (i == 88) {
            fill(220);
            textSize(23);
            noStroke();
            textAlign(RIGHT);
            text(MonName[dateMonth] + " " + dateDate + ", " + dateHour + ":00", 1040, 29 + graphNumber);
            shadeHour = dateHour;
        }
    } //end time lable section
    
Sign In or Register to comment.