How to set a variable as Date type in ejs?

I'm using express.js, mongoose and ejs, I have a Model like

var t_schema = new mongoose.Schema({
  name: String,
  startFrom: Date,
  ...
})

router.get('/', function(req,res) {
 t_model.findOne({name: req.body.name}, function(result) {res.render('t', result)})
})

So result contains a Date type startFrom

And in my ejs :

$('#startDate').datepicker.set(<%= result.startDate %>)

which is a String instead of Date, while datepicker.set needs Date.

How can I make result.startDate as a Date?

$('#startDate').datepicker.set(new Date('<%= result.startDate %>'))

seems work, but the timezone is incorrect.

And 2.)

What is the best practice to manipulate Date in Nodejs/mongodb? My users will come from different timezone, so I would like to show datetime in their timezone in Browser, I think maybe its better to store date as UTC in Mongodb?

Answers


To answer question 2 first, yes, it would be better to store date as UTC in mongo, then when displayed to the individual user, convert to localtime based on user browser timezone. Or not do any conversions and just say that the time is in UTC.

To answer the first question, your jquery datepicker can be set with:

Date date = new Date('<%= result.startDate %>'); 
$('#startDate').datepicker('setDate', date.toUTCString());

Need Your Help

accessing D3 dynamical json object in Rails view

javascript ruby-on-rails json d3.js coffeescript

I am learning D3 and to start, I try to display a graph where I hard-code the json data