草庐IT

ios - 比较日期与 currentDate Swift 时的奇怪 react

coder 2023-09-07 原文

当我将日期与 currentDate 进行比较时,正如标题所说,我有一个非常奇怪的 react 。 这是发生了什么: 我查询我的服务器以获取一些日期,然后打印它们,这样我就可以确保它们是正确的(一切都很好)。 然后我打印 3 小时前出现的 currentDate。 好的,我会修复它,然后我说。 但!当我尝试比较它们时,我只使用 20:59 和更早的日期。

这是我的代码(//dateevent 是我从服务器恢复的日期)

if dateevent.earlierDate(self.currentDate).isEqualToDate(self.currentDate){
   print("All dates \(dateevent)")
   if NSCalendar.currentCalendar().isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day){
     print("here is what it passed from server \(dateevent)")
     print("here is the current date \(self.currentDate)") 
                        }

                    }

这是我的输出

All dates 2016-07-28 19:00:00 +0000
here is what it passed from server 2016-07-28 19:00:00 +0000
here is the current date 2016-07-28 13:43:51 +0000

All dates 2016-07-28 19:00:00 +0000
here is what it passed from server 2016-07-28 19:00:00 +0000
here is the current date 2016-07-28 13:43:51 +0000

All dates 2016-07-28 21:00:00 +0000
All dates 2016-07-28 21:00:00 +0000
All dates 2016-07-28 23:30:00 +0000
All dates 2016-07-29 21:00:00 +0000
All dates 2016-07-29 22:30:00 +0000
All dates 2016-07-29 23:00:00 +0000
All dates 2016-07-29 23:00:00 +0000
All dates 2016-07-29 23:30:00 +0000
All dates 2016-07-30 21:00:00 +0000

最佳答案

我不打算讨论你应该在服务器端使用什么时区,但最方便和一致的方式是 UTC。由于您已经在服务器上使用 UTC 时区,因此在服务器上存储日期和时间时需要考虑到这一点。我的意思是,如果您要存储例如2016-07-28 21:00:00 +0000 , 它不一定转化为 2016-07-28 21:00:00在您的位置。

请参阅以下内容:

let time = NSDate()        // Create an object for the current time.

print(time, "\n")          // Sample output: 
                              2016-07-28 15:23:02 +0000

time按原样打印出来的对象以 UTC 格式输出当前时间。作为引用,我的本地时区也恰好是UTC+3,所以本地时间是2016-07-28 18:23:02 +0300 .

接下来我们来看几个字符串格式的日期:

let strings = [
    "2016-07-28 19:00:00 +0000",
    "2016-07-28 20:00:00 +0000",
    "2016-07-28 20:59:59 +0000", // Second before midnight, UTC+3.
    "2016-07-28 21:00:00 +0000", // Midnight in UTC+3.
    "2016-07-28 22:00:00 +0000",
    "2016-07-28 23:30:00 +0000",
    "2016-07-28 23:59:59 +0000", // Second before midnight, UTC.
    "2016-07-29 00:00:00 +0000"  // Midnight in UTC.
]

现在,让我们将这些字符串转换为 NSDate对象,再次将保留在 UTC 时区:

var dates: [NSDate] = []

for string in strings {
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
    guard let date = formatter.dateFromString(string) else { continue }
    dates.append(date)
}

接下来,我们将转换 NSDate将对象转换为具有本地格式的字符串,这可能是您混淆的根源:

for date in dates {
    print("Current time in UTC: ", time)
    print("Date in UTC:         ", date)

    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    formatter.timeZone = NSTimeZone.localTimeZone()
    print("Current local time:  ", formatter.stringFromDate(time))
    print("Date in local time:  ", formatter.stringFromDate(date))
}

// Sample output for the date 2016-07-28 19:00:00 +0000:
// Current time in UTC:  2016-07-28 15:23:02 +0000
// Date in UTC:          2016-07-28 19:00:00 +0000
// Current local time:   2016-07-28 18:23:02
// Date in local time:   2016-07-28 22:00:00

关于ios - 比较日期与 currentDate Swift 时的奇怪 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38638413/

有关ios - 比较日期与 currentDate Swift 时的奇怪 react的更多相关文章

  1. ruby - Ruby 的 Hash 在比较键时使用哪种相等性测试? - 2

    我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。

  2. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  3. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  4. ruby - 检查日期是否在过去 7 天内 - 2

    我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/

  5. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  6. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案: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

  7. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个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

  8. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  9. ruby-on-rails - ruby 日期方程不返回预期的真值 - 2

    为什么以下不同?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

  10. ruby-on-rails - 事件管理员日期过滤器日期格式自定义 - 2

    是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s

随机推荐