I have a date object that I want to reformat to a different date style, but am having trouble doing so. I've tried .format()
and using moment
, but can't get it to work (.format()
just throws a start.format("YYYY/DDD") is not a function
error and moment
seems to only return Unix timestamps no matter what I do. I keep getting Invalid Date
. Here is a fiddle with my code in it (the dates are displayed when you hover over a block of time).
As requested, here is the actual code:
google.setOnLoadCallback(drawChart);
function drawChart() {
var container = document.getElementById('timeline');
var chart = new google.visualization.Timeline(container);
var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'string', id: 'RowLabel' });
dataTable.addColumn({ type: 'string', id: 'BlockLabel' });
dataTable.addColumn({ type: 'date', id: 'Start' });
dataTable.addColumn({ type: 'date', id: 'End' });
dataTable.addRows([
["SAT 2B", "Task 1", new Date(2015, 01, 01), new Date(2015, 02, 02)],
["SAT 2B", "Task 2", new Date(2015, 01, 05), new Date(2015, 01, 08)],
["SAT 2B", "Task 1", new Date("2015-03-10 05:10:39.010000"), new Date("2015-03-15 05:10:39.010000")],
["SAT 2C", "Task 1", new Date("2015-04-10 05:10:39.010000"), new Date("2015-04-15 05:10:39.010000")]
]);
var options = {
timeline: { colorByRowLabel: true }
};
chart.draw(dataTable, options);
google.visualization.events.addListener(chart, 'onmouseover', function(e) {
setTooltipContent(dataTable,e.row);
});
}
function setTooltipContent(dataTable,row) {
if (row != null) {
var sat = dataTable.getValue(row,0);
var taskID = dataTable.getValue(row,1);
//var start = dataTable.getValue(row,2);
var start = dataTable.getValue(row,2).toDateString();
var startFormatted = new Date(start, "dd.mm.yyyy hh:MM:ss")
//start.format("dddd, MMMM Do YYYY, h:mm:ss a");
//start.format('yyyy/DDD-HH:mm:ss');
var end = dataTable.getValue(row,3);
var content = '<div class="custom-tooltip" ><h1>' + sat + ','+ taskID +'</h1><div>' +'Start:'+startFormatted +', End: '+ end+'</div></div>'; //generate tooltip content
var tooltip = document.getElementsByClassName("google-visualization-tooltip")[0];
tooltip.innerHTML = content;
}
}
To the best of my knowledge what I am trying to reformat is a date object so I'm not sure what is going wrong. I want it to display in the YYYY/DDD HH:mm:ss
format (where DDD
is day of year).