简体   繁体   English

bootstrap-datepicker:如何使用特定时区?

[英]bootstrap-datepicker: How to use specific time zone?

I created a small program to select the date using bootstrap-datepicker and write it to MySQL. 我创建了一个小程序,使用bootstrap-datepicker选择日期并将其写入MySQL。

The catch is that this date must be local to Europe/Berlin regardless where the user is at the moment. 问题是这个日期必须是Europe/Berlin本地日期,无论用户目前在哪里。

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

My question is: how do I set startDate to be local to specific timezone 我的问题是:如何将startDate设置为特定时区的本地 without using server side code (PHP) or any other libraries 不使用服务器端代码(PHP)或任何其他库 ?

I tried using moment + moment-timezone but it's also showing the local browser date. 我尝试使用moment + moment-timezone但它也显示了本地浏览器日期。

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

Thanks. 谢谢。

Just needed 只需要

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

After which moment generated the correct date without using tz() . 在此之后,不使用tz()生成正确的日期。 Also make sure that format matches. 还要确保格式匹配。

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

The date object will always be in Local time zone (browser based). 日期对象将始终位于本地时区(基于浏览器)。

dateObj.getTime() will give you absolute milliseconds. dateObj.getTime()将给你绝对的毫秒数。

http://momentjs.com/ is great library for timezone conversions. http://momentjs.com/是时区转换的绝佳库。

EDIT 编辑

About how: 如何:

 var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
 var losAngeles = newYork.clone().tz("America/Los_Angeles");
 var london     = newYork.clone().tz("Europe/London");

 newYork.format();    // 2014-06-01T12:00:00-04:00
 losAngeles.format(); // 2014-06-01T09:00:00-07:00
 london.format();     // 2014-06-01T17:00:00+01:00

and

By default, moment parses and displays in local time. 默认情况下,时刻会以本地时间进行分析和显示。

This is the exact code from their website. 这是他们网站上的确切代码。 If you don't pass America/New_York and just simply moment("2014-06-01 12:00") it will get local time. 如果你没有通过America/New_York ,只是moment("2014-06-01 12:00") ,它将获得当地时间。

moment().utcOffset() function will get you utcOffset if you want to know which timezone moment interprets. moment().utcOffset()如果你想知道哪个时区解释, moment().utcOffset()函数会给你utcOffset。

See the demo in jsFiddle 请参阅jsFiddle中的演示

Maybe something like this can get you in the right direction? 也许像这样可以让你在正确的方向?

HTML HTML

<div class='input-group date date-time-picker'>
    <input type='text' class="form-control" placeholder="Start Time"/>
    <span class="input-group-addon">
        <i class="glyphicon glyphicon-calendar"></i>
    </span>
</div>

JavaScript JavaScript的

var options = {
    format: 'mm/dd/yyyy hh:ii',
    autoclose: true
};

$('.date-time-picker').datetimepicker(options).on("changeDate", function (e) {
    var TimeZoned = new Date(e.date.setTime(e.date.getTime() + 
                            (e.date.getTimezoneOffset() * 60000)));
    $(this).datetimepicker('setDate', TimeZoned);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM