创建日期时,瞬间js默认时区不起作用(Moment js default timezone not working when creating date)

请参阅以下代码片段。

问题1:我正在为时刻js设置默认时区。 但是在设置之后,如果我从一刻开始检索时区,那就不是我刚设置的时区。

console.log("Currrent timezone: "); console.log(moment.tz());
console.log("Updating timezone: "); console.log("America/Lima");
moment.tz.setDefault("America/Lima");
console.log("Currrent timezone after updating: "); console.log(moment.tz());

以下日志被打印出来:

Currrent timezone: Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Nov 15 2017 13:39:45 GMT+0500 (Pakistan Standard Time), …}
Updating timezone: America/Lima
Currrent timezone after updating: Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Nov 15 2017 13:39:45 GMT+0500 (Pakistan Standard Time), …}

问题2:另一个问题是,我设置的默认时区应用于moment.format()函数,但从当前创建新的日期对象时不应用。

console.log(moment(new Date()).toDate()); // this uses user browser timezone
// prints Wed Nov 15 2017 13:57:40 GMT+0500 (Pakistan Standard Time)
// I want it to give the time in the timezone which i just set above

console.log(moment(new Date()).local().toDate());
// prints Wed Nov 15 2017 13:57:40 GMT+0500 (Pakistan Standard Time)

console.log(moment(new Date()).format()); // this uses the timezone which I set above in moment default timezone. This is correct intended behavior
// prints: 2017-11-15T03:57:40-05:00

Please refer to the following code snippets.

Problem 1: I am setting default timezone for moment js. But right after setting it, If I retrieve the timezone from moment it is not what I just set.

console.log("Currrent timezone: "); console.log(moment.tz());
console.log("Updating timezone: "); console.log("America/Lima");
moment.tz.setDefault("America/Lima");
console.log("Currrent timezone after updating: "); console.log(moment.tz());

Following log is printed:

Currrent timezone: Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Nov 15 2017 13:39:45 GMT+0500 (Pakistan Standard Time), …}
Updating timezone: America/Lima
Currrent timezone after updating: Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Nov 15 2017 13:39:45 GMT+0500 (Pakistan Standard Time), …}

Problem 2: Also another problem is that, the default timezone which I set is applied to moment.format() function but not applied when creating new date objects from moment.

console.log(moment(new Date()).toDate()); // this uses user browser timezone
// prints Wed Nov 15 2017 13:57:40 GMT+0500 (Pakistan Standard Time)
// I want it to give the time in the timezone which i just set above

console.log(moment(new Date()).local().toDate());
// prints Wed Nov 15 2017 13:57:40 GMT+0500 (Pakistan Standard Time)

console.log(moment(new Date()).format()); // this uses the timezone which I set above in moment default timezone. This is correct intended behavior
// prints: 2017-11-15T03:57:40-05:00

原文:https://stackoverflow.com/questions/47303375
2024-04-23 11:04

满意答案

{Binding Key}{Binding Path=Key}简写。 这将返回当前DataContextKey属性的值。


{Binding Key} is shorthand for {Binding Path=Key}. This will return the value of the Key property on the current DataContext.

相关问答

更多

silverlight在绑定中的表达(silverlight expression in binding)

您不能绑定到Binding中的多个源属性。 因此,您需要某种聚合器,它提供可以绑定的输出属性。 以下是相同模式的一些变体: <UserControl.Resources> <sys:Double x:Key="width">10</sys:Double> <sys:Double x:Key="margin">30</sys:Double> <BindableResult x:Key="widthPlusMargin" ArithmeticOperation="Add" Le...

Silverlight {Binding}不会访问类成员(Silverlight {Binding} won't access class members)

您应该使用属性,而不是字段。 public class ChatMessage { public string Message { get; set; } public DateTime DateReceived { get; set; } public override string ToString() { return Message; } public ChatMessage() { Message...

Keylight上的Silverlight双向数据绑定(Silverlight Two Way Data Binding on Key Up)

您还可以使用Blend交互行为来创建可重用的行为,以更新KeyUp上的绑定,例如: public class TextBoxKeyUpUpdateBehaviour : Behavior<TextBox> { protected override void OnAttached() { base.OnAttached(); AssociatedObject.KeyUp += AssociatedObject_KeyUp; } v...

Silverlight 4中的WS绑定(WS Binding in Silverlight 4)

除了basicHttp和webHttp(REST)之外,SL4还将支持netTcp绑定。 参见: http://www.silverlightshow.net/items/WCF-NET.TCP-Protocol-in-Silverlight-4.aspx SL4 will support the netTcp binding in addition to basicHttp and webHttp (REST). See : http://www.silverlightshow.net/item...

如何防止绑定刷新(How To Prevent Binding Refresh)

像这样的东西。 public string stringProperty { get { return _stringProperty; } set { if (!_stringProperty.Equals(value)) { _stringProperty = value; OnPropertyChanged("stri...

Silverlight {Binding Key} - 请刷新我的记忆?(Silverlight {Binding Key} - Please refresh my memory?)

{Binding Key}是{Binding Path=Key}简写。 这将返回当前DataContext上Key属性的值。 {Binding Key} is shorthand for {Binding Path=Key}. This will return the value of the Key property on the current DataContext.

SilverLight Datagrid刷新(SilverLight Datagrid refresh)

在您的视图模型中为CurrentItem / Entity创建一个属性,如下所示: private Customer customer; public Customer CurrentCustomer { get { return this.customer; } set { if (this.customer!= value) { this.cu...

Silverlight内存泄漏(Silverlight Memory Leak)

好的,所以我们实际上刚刚购买了用于Silverlight的Telerik工具,但是如果你真的想解决这个问题,你可以查看工具包的源代码(2009年11月,应该修复新版本)然后只需取消订阅事件处理程序关闭日历弹出窗口后。 Ok, so we actually just bought the Telerik Tools for silverlight but if you really want to go through the trouble you can go look in the sourc...

Image Binding,Silverlight,C#,WP7(Image Binding, Silverlight, C#, WP7)

它永远不会被调用,因为您的属性不会引发PropertyChanged通知。 您需要将其设置为DependencyProperty或实现INotifyPropertyChanged并在设置属性值时引发PropertyChanged事件。 It never gets called because your property does not raise a PropertyChanged notification. You need to either make it a DependencyProp...

Silverlight ListboxItem可见性绑定(Silverlight ListboxItem Visibility binding)

WPF已经附带了一个开箱即用的BooleanToVisibilityConverter。 所以简单地定义如下: <UserControl.Resources> <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/> </UserControl.Resources> 希望这可以帮助 WPF already comes with a BooleanToVisibilityConverter out of the...

相关文章

更多

Working on Free Software

This was written in December, 1999 Lots of programm ...

在java.util.Date中,如何比较两个Date的大小,精确到分

如题。。麻烦给位了 问题补充:是的哦 renpeng301 写道 晕看来理 ...

如何加载JS,JS应该放在什么位置?-高性能web开发

所有浏览器在下载JS的时候,会阻止一切其他活动,比如其他资源的下载,内容的呈现等等。至到JS下载、解析 ...

强力推荐几款JS插件,

强力推荐几款JS插件, Advanced Register/Signup ...

两种js function 声明方式

http://helephant.com/2012/07/14/javascript-function ...

jQuery表格插件jqGrid(jquery.jqGrid.js)

jqGrid 是一个用来显示网格数据的jQuery插件,文档比较全面,附带中文版本,支持分页、滚动加载 ...

ant打包时自动压缩js和css

使用ant打包发布WAR时,可以对js和css进行自动压缩,主要是使用了yuicompressor-2 ...

用SimpleDateFormat把字符串转换成Date的时候报NumberFormatException

今天用for循环产生多条线程去缓存里面取数据,数据就是字符串形式的日期,我如果用for循环连续的产生线 ...

Backbone.js的技巧和模式

本文由白牙根据Phillip Whisenhunt的《Backbone.js Tips And Pat ...

js中2个等号与3个等号的区别

js中2个等号与3个等号的区别 2011-06-24 14:14:09|分类:javascrip ...

最新问答

更多

python的访问器方法有哪些

使用方法: class A(object): def foo(self,x): #类实例方法 print "executing foo(%s,%s)"%(self,x) @classmethod def class_foo(cls,x): #类方法 print "executing class_foo(%s,%s)"%(cls,x) @staticmethod def static_foo(x): #静态方法 print "executing static_foo(%s)"%x调用方法: a =

使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)

我认为你必须将两个TableGetway传递给UserTable构造。 你必须改变Module.php看看: public function getServiceConfig() { return array( 'factories' => array( 'User\Model\UserTable' => function($sm) { $userTableGateway = $sm->get('UserTable

透明度错误IE11(Transparency bug IE11)

这是一个渲染错误,由使用透明度触发,使用bootstrap用于在聚焦控件周围放置蓝色光环的box-shadow属性。 可以通过添加以下类覆盖来解决它。 .form-control:hover { -webkit-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); -moz-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); box-shadow: 0px 0px 4px 0px rgba(0,0,255,1)

linux的基本操作命令。。。

ls 显示目录 mkdir 建立目录 cd 进入目录

响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)

将z-index设置为.main-nav这将解决您的重叠问题 .main-nav { position:relative; z-index:9; } set z-index to .main-nav This will fix your overlaping issue .main-nav { position:relative; z-index:9; }

在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)

这是因为模式规范"a"打开一个文件以便追加,文件指针在末尾。 如果您尝试从此处读取,则由于文件指针位于EOF,因此没有数据。 您应该打开"r+"进行阅读和写作。 如果在写入之前读取整个文件,则在写入更多数据时,文件指针将正确定位以追加。 如果这还不够,请探索ftell()和fseek()函数。 That is because the mode spec "a" opens a file for appending, with the file pointer at the end. If you

NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)

支持空中接口的数据速率是一回事。 在消除协议开销,等待eeprom写入以及所有需要时间的其他内容之后,您看到的数据速率是完全不同的故事。 长话短说,从标签读取或进行对等传输时的实际数据速率峰值约为2.5千字节/秒。 取决于具体的标签或对等技术,它可能比这慢很多。 The supported data-rates of the air-interface are one thing. The data-rate that you see after removing protocol overhe

元素上的盒子阴影行为(box-shadow behaviour on elements)

它看起来像只在Windows上的Chrome的错误。 我在Google Canary (Chrome 63)中也进行了测试,问题依然存在,所以有可能它不会很快修复。 这个问题是由overflow: auto引起的overflow: auto ,在你的情况下,它可以很容易地通过删除或设置为可见(默认)来解决。 但是 ,将鼠标悬停在右侧(顶部和底部)时,会出现滚动条。 一个解决方案可以设置overflow: hidden的身体,所以预期的结果是所需的。 我想指出,这不是一个很好的解决方案,但我建议暂

Laravel检查是否存在记录(Laravel Checking If a Record Exists)

这取决于您是否要以后与用户合作,或仅检查是否存在。 如果要使用用户对象(如果存在): $user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist } 如果你只想检查 if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found

设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)

$scope.getData= function () { var reader = new FileReader(); reader.onload = $('input[type=file]')[0].files; var img = new Image(); img.src =(reader.onload[0].result); img.onload = function() { if(this.width > 640