Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting date using milliseconds

I currently have a date picker on the client side. Once a date is selected the date in milliseconds is sent to my node app. the problem is i am getting Invalid Date for new Date(milliseconds)

the milliseconds sent look like this ( 1347433200000 ) my code is as fallows

app.get('/dashboard/date/:date', function(req, res){
    console.log(new Date(req.params.date));
    var start = new Date(req.params.date);
    var end = new Date(req.params.date).add({hours:23, minutes:59, seconds: 59, milliseconds: 999});
    console.log(start);
    console.log(end);

    Appointments.find({'scheduled' : {"$gte": start, "$lt": end}}, function(err, list){
        res.render('templates/list',{ layout: false, appointments: list });
    });
});
like image 245
Vartan Arabyan Avatar asked Sep 18 '12 18:09

Vartan Arabyan


People also ask

How do you convert milliseconds to dates?

Use the Date() constructor to convert milliseconds to a date, e.g. const date = new Date(timestamp) . The Date() constructor takes an integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 UTC and returns a Date object.

How do you set the time in milliseconds?

Java Calendar setTimeInMillis() MethodThe setTimeInMillis () method of java. util. Calendar class is used to set the current time in millisecond. The date value to be set is passed as long into this method.

How do you format milliseconds?

In the Format Cells window, go to the Number tab, select Custom from the Category list, and enter h:mm:ss. 000 in the Type text box. As a result, all of the time values are displayed with milliseconds as decimals.


1 Answers

req.params.date is a string so you need to convert it to a number before passing it to the Date constructor. Try this instead:

var start = new Date(Number(req.params.date));
like image 102
JohnnyHK Avatar answered Sep 26 '22 10:09

JohnnyHK