bootstrap-datepicker: How to use specific time zone?

Alex G picture Alex G · Nov 25, 2015 · Viewed 45k times · Source

I created a small program to select the date using bootstrap-datepicker and write it to MySQL.

The catch is that this date must be local to Europe/Berlin regardless where the user is at the moment.

$(....).datepicker({ startDate: 'today' });

My question is: how do I set startDate to be local to specific timezone without using server side code (PHP) or any other libraries?

I tried using moment + moment-timezone but it's also showing the local browser date.

var todaysDate = moment().tz('Europe/Berlin').format();
$(...).datepicker({ startDate: todaysDate });

Thanks.

Answer

Alex G picture Alex G · Nov 30, 2015

Just needed

moment.tz.setDefault("Europe/Berlin");

After which moment generated the correct date without using tz(). Also make sure that format matches.

var todaysDate = moment().format('D MMM YYYY');
$(...).datepicker({ format: 'dd M yyyy', startDate: todaysDate });