假设用户在与应用程序服务器不同的时区从他的浏览器运行我的网络应用程序。我使用 JavaScript 的 date.getTime() 方法在客户端序列化一个日期。我通过 Json 发送生成的毫秒数,然后通过调用 new Date(millisecondsFromJS) 在服务器端创建一个 Java Date 对象。我将它存储在 MySql 上,检索它,通过调用 Java 的 date.getTime() 再次序列化它,并通过 Json 再次将它发送给客户端。
如果我用这些毫秒创建一个 JavaScript Date 对象,它会得到原始日期吗?我已经成功完成了这个过程,但客户端和服务器目前处于同一时区。如果时区不同,我不确定日期是否会在此过程中损坏。
据我了解,使用 getTime() 会返回一个独立于时区的即时时间。例如,如果用户在 CDT 2012 年 7 月 17 日下午 4:39 捕获,服务器可能会将其存储为 CEST 2012 年 7 月 17 日晚上 11:39,但是一旦服务器将其转换为自 GMT 以来的毫秒数并且客户端创建从这些毫秒开始,它将成功重建原始的 2012 年 7 月 17 日下午 4:39 CDT。这是真的吗?
最佳答案
文章中有一些很好的建议 Scaling lessons learned at Dropbox, part 1 :
Keep everything in UTC internally! Server times, stuff in the database, etc. This will save lots of headaches, and not just daylight savings time. Some software just doesn’t even handle non-UTC time properly, so don’t do it! We kept a clock on the wall set to UTC. When you want to display times to a user, make the timezone conversion happen at the last second.
将unix时间毫秒发送到服务器,你就知道用户选择了哪个时间点。然后以 UTC 处理服务器上的所有内容,并将毫秒整数返回给客户端。
客户端/JavaScript:
var date = new Date();
var clientMilliseconds = date.getTime();
// send clientMilliseconds to server
服务器/Java:
Date date = new Date(clientMilliseconds);
// store the date, then get it back
long serverMilliseconds = date.getTime();
// send serverMilliseconds back to client
客户端/JavaScript:
var date = new Date(serverMilliseconds);
// If receiving the error "Invalid Date", serverMilliseconds
// needs to be converted to an Integer. Consider:
// parseInt: parseInt(serverMilliseconds, 10)
// unary +: (+serverMilliseconds)
在此过程中,服务器和客户端上的 date 对象都将反射(reflect)各自的时区,因此如果您查看两者,它们可能看起来不同,但如果您转换它们则不然他们使用相同的时区回到 UTC。
所以回答你的问题:
If I create a JavaScript Date object with those milliseconds, will it result in the original date?
是的。
Java 的 Date(long date) constructor和 getTime() method使用 unix 时间毫秒操作。 JavaScript 的 getTime() 也是如此。和 Date constructor .除 Coordinated Universal Time 外,不应涉及其他时区(格林威治标准时间/协调世界时)。
关于Java/JavaScript 日期 : Is this true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11531291/
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht