DEVOPS
DEVOPS

Reputation: 18790

How to convert a time to javascript-timestamps using javascript or jquery?

How I will convert this time 11:34 to javascript timestamps. Any javascript functionality available for that.

I am trying to create a floting graph for that I am using Flot library. In my graph time on x axis and count on y axis. For creating data part I need to convert time to timestamp like that thay specified on API doc.

http://people.iola.dk/olau/flot/API.txt

This is my code

var datasets = {
    "usa": {
        label: "Logged Users",
        data: [[10:55, 4], [11:00, 1], [11:05, 4], [11:10, 2], [11:15, 3], [11:20, 1], [11:25, 5]]
    }
};

if (datasets.length > 0){
    $.plot($("#placeholder"), datasets, {
        yaxis: { min: 0,tickDecimals: 0 },
        xaxis: {  mode: "time",timeformat: "%H:%M" }
    });
}

It will not work because I specified exact time instead of a number. So I need to convert that to timestamps format.
Please help me.

Thanks

Upvotes: 2

Views: 5122

Answers (3)

scessor
scessor

Reputation: 16115

Use an instance of the Date object:

var sTime = '11:34';
var oDate = new Date();
oDate.setUTCHours(
    parseInt(sTime.substr(0, 2), 10),
    parseInt(sTime.substr(3, 2), 10),
    0,
    0
);
var sTimestamp = oDate.getTime();

Also see this example.

=== UPDATE ===

When the time is local time instead of UTC, then you can set the time with:

oDate.setHours(
    parseInt(sTime.substr(0, 2), 10),
    parseInt(sTime.substr(3, 2), 10),
    0,
    0
);

Also see this example.

P.s.: the result of getTime() is in milliseconds.

=== UPDATE ===

To map your current dataset you can use following script (with UTC; if you want local time remove the UTC in the setter):

var aCountries = [ "usa" ];
var oDate = new Date();
oDate.setSeconds(0, 0);
for (var i = 0; i < aCountries.length; i++) {
    datasets[aCountries[i]].data =
        datasets[aCountries[i]].data.map(function(oElement) {
            oDate.setUTCHours(
                parseInt(oElement[0].substr(0, 2), 10),
                parseInt(oElement[0].substr(3, 2), 10)
            );
            return [
                oDate.getTime()
                , oElement[1]
            ];
        });
}

Also see this example.

Upvotes: 4

Kamlesh
Kamlesh

Reputation: 609

I've found the jQuery Globalization Plugin date parsing to work best. Other methods had cross-browser issues and stuff like date.js had not been updated in quite a while.

You also don't need a datePicker on the page. You can just call something similar to the example given in the docs:

$.parseDate('yy-mm-dd', '2007-01-26');

Upvotes: -1

Kamlesh
Kamlesh

Reputation: 609

    var now = new Date();

    now.format("m/dd/yy");

// Returns, e.g., 6/09/07

// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM

// You can use one of several named masks
now.format("isoDateTime");
// 2007-06-09T17:46:21

// ...Or add your own
dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
now.format("hammerTime");
// 17:46! Can't touch this!

// When using the standalone dateFormat function,
// you can also provide the date as a string
dateFormat("Jun 9 2007", "fullDate");
// Saturday, June 9, 2007

// Note that if you don't include the mask argument,
// dateFormat.masks.default is used
now.format();
// Sat Jun 09 2007 17:46:21

// And if you don't include the date argument,
// the current date and time is used
dateFormat();
// Sat Jun 09 2007 17:46:22

// You can also skip the date argument (as long as your mask doesn't
// contain any numbers), in which case the current date/time is used
dateFormat("longTime");
// 5:46:22 PM EST

// And finally, you can convert local time to UTC time. Either pass in
// true as an additional argument (no argument skipping allowed in this case):
dateFormat(now, "longTime", true);
now.format("longTime", true);
// Both lines return, e.g., 10:46:21 PM UTC

// ...Or add the prefix "UTC:" to your mask.
now.format("UTC:h:MM:ss TT Z");
// 10:46:21 PM UTC

Use all date time example .,so use it.

Upvotes: 0

Related Questions