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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33918017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:39:16  来源:igfitidea点击:

bootstrap-datepicker: How to use specific time zone?

javascriptjquerymomentjsbootstrap-datepicker

提问by Alex G

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

我创建了一个小程序来使用bootstrap-datepicker 选择日期并将其写入 MySQL。

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

问题是这个日期必须是本地的,Europe/Berlin无论用户现在在哪里。

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

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

我的问题是:如何startDate将本地设置为特定时区不使用服务器端代码 (PHP) 或任何其他库?

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

我尝试使用moment+moment-timezone但它也显示本地浏览器日期。

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

Thanks.

谢谢。

回答by Alex G

Just needed

刚需

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

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

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

在那一刻之后生成了正确的日期而不使用tz(). 还要确保格式匹配。

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

回答by Akshay

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_Yorkand 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()如果您想知道哪个时区时刻解释,函数将为您提供 utcOffset 。

See the demo in jsFiddle

参见jsFiddle 中的演示

回答by Arg0n

Maybe something like thiscan 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);
});