0

I'm having trouble figuring out how to take a datestamp and converting it to a readable date. I've tried different methods but I seem to be missing something. I'm able to get the array, loop through the values and grab what I need, I just can't seem to convert the timestamp properly (at all really).

Here is my full code with a few notes:

<script>
        var ctx = $("#ertChart").get(0).getContext("2d");
        var myNewChart = new Chart(ctx);

        var ert = @Html.Raw(Json.Encode(Model));  // contains [{""H":"E","consumption":0.46,"readingdatetime":"\/Date(1410678000000)\/"}]

        var ertLabel = []; 
        var ertValue = [];

        for (var i in ert) {
            ertLabel.push(ert[i].formatdate('yy-mm-dd', readingdatetime));  // << this is where I've been playing with the date formatting in different ways
            ertValue.push(ert[i].consumption);
        }

        var ertLabelString = ertLabel.join(",");
        var ertValueString = ertValue.join(",");
        blah
        var data = {
            labels: [ertLabelString],
            datasets: [
                {
                    label: "My First dataset",
                    fillColor: "rgba(220,220,220,0.2)",
                    strokeColor: "rgba(220,220,220,1)",
                    pointColor: "rgba(220,220,220,1)",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(220,220,220,1)",
                    data: [ertValueString]
                }
            ]
        }
        new Chart(ctx).Line(data);
    </script>

What I'm trying to do is take readingdatetime and convert it to a readable date in

mm-dd-yyy HH:MM format if H = E

or

HH:MM if H = I

Can anyone point me in the right direction?

Here is where the modified code blows up:

var ert = @Html.Raw(Json.Encode(Model));

        var ertLabel = []; 
        var ertValue = [];

        for (var i in ert) {
            var dateString = moment(ert[i].readingdatetime).format("MM-DD-YYYY HH:mm");
            ertValue.push(ert[i].consumption);
        }

        var ertLabelString = dateString.join(",");  // << Exception property or method not valid 'Join'
        var ertValueString = ertValue.join(",");
Schnazzer
  • 1
  • 2

2 Answers2

0

Something like that ?

http://jsfiddle.net/OxyDesign/vs2e618L/

JS

var date = new Date(1410678000000),
    month = date.getMonth()+1, //mm
    day = date.getDate(), //dd
    year = date.getFullYear(), //yyyy
    hours = date.getHours(), //HH
    minutes = date.getMinutes(), //MM
    dateString;

if(month <= 9) month = '0'+month;
if(day <= 9) day = '0'+day;
if(hours <= 9) hours = '0'+hours;
if(minutes <= 9) minutes = '0'+minutes;

dateString = month+'-'+day+'-'+year+' '+hours+':'+minutes;

console.log(dateString);
OxyDesign
  • 754
  • 5
  • 6
  • ok, so I tried what you have above but instead of 1410678000000 explicit, I added ert[I].readingdatetime to pull that current row data.. it grabs /(date)1410678000000/ from the array and at the end of the machine it spits out NaN-NaN-NaN NaN:NaN – Schnazzer Sep 26 '14 at 17:15
  • I notice that in the array, the date is listed as /Date(some time value)/ I'm not sure how to work with this – Schnazzer Sep 26 '14 at 17:18
  • Yes it's weird ... if you're sure about the format, you can do something like that `new Date(parseInt(ert[I].readingdatetime.replace(/\D/g,'')))` – OxyDesign Sep 26 '14 at 17:36
0

You will find that formatting dates in JavaScript is non-trivial. Consider using a library, such as moment.js, which makes things much easier, and covers edge cases you may not have thought about.

moment(readingdatetime).format("MM-DD-YYYY HH:mm")
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • this is much simpler code but I find I'm still stuck in the same spot.. within the array ert, date is listed as /Date(some time stamp)/ using your code, I still get the same output "/Date(some time stamp/" My real problem it seems is figuring out how to handle something like "/Date(1410678000000)/" vs. just "1410678000000" – Schnazzer Sep 26 '14 at 17:30
  • Moment can parse dates in that format. See [these docs](http://momentjs.com/docs/#/parsing/asp-net-json-date/). – Matt Johnson-Pint Sep 26 '14 at 17:51
  • Also - You appear to be using JSON directly as a JS Object. You need to wrap it in a call to `JSON.parse`. See [here](http://stackoverflow.com/a/6487185/634824) – Matt Johnson-Pint Sep 26 '14 at 17:54
  • I'm assuming you mean something like this? var ert = JSON.parse(@Html.Raw(Json.Encode(Model))); which mostly works.. I see the array as before but I get a Invalid Character error – Schnazzer Sep 26 '14 at 18:07
  • Now I'm getting "moment is undefined" from var dateString = moment(ert[i].readingdatetime).format("MM-DD-YYY HH:mm"); – Schnazzer Sep 26 '14 at 18:14
  • ok, I just need to read more carefully.. I grabbed moment.js and now its formatting the date.. but, I get an error now: JavaScript runtime error: Object doesn't support property or method 'join' I'll post the code here.. – Schnazzer Sep 26 '14 at 18:22
  • Please limit your question to the exact problem at hand. Stack Overflow is not an interactive debugger. :) See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Matt Johnson-Pint Sep 26 '14 at 18:26
  • And `join` is for arrays - not for strings. It's unclear why you want that. – Matt Johnson-Pint Sep 26 '14 at 18:27
  • I realize now that I missed an important piece of information when submitting the question. What I'm trying to do is take that JSON array and creating 2 separate arrays (comma separated by the join) one adds labels and the other adds data to a chart. I was able to grab the data just fine, I ran into a problem trying to convert the timestamp to a date before creating the ertLabel array. – Schnazzer Sep 26 '14 at 18:37
  • Sorry - Not following you. You need an MCVE. Please read the link I posted above. We appreciate new users at S.O., but you need to follow the rules if you want good help. – Matt Johnson-Pint Sep 26 '14 at 18:47