无法从swift 3中的2个时间戳日期中获取日期之间的日期

时间:2017-11-21 12:57:48

标签: ios iphone xcode date

我从服务器响应中获得两个时间戳。喜欢以下

    value = 1507824000; //2017-10-12 16:00:00 +0000
    value2 = 1507939200; //2017-10-14 00:00:00 +0000

      let startTimeStamp = dateTimeStampValues["value"] as! String
      let endTimeStamp = dateTimeStampValues["value2"] as! String


     let convertedDate = Double(startTimeStamp)

     let convertedEndDate = Double(endTimeStamp)

     var startdate = Date(timeIntervalSince1970: convertedDate!) 


     let enddate = Date(timeIntervalSince1970: convertedEndDate!) 
    self.showRange(between: startdate, and: enddate)

func showRange(between startDate: Date, and endDate: Date) {
    guard startDate < endDate else { return }

    let calendar = Calendar.current
    let calendarEndDate = calendar.startOfDay(for: endDate)

    var currentDate = calendar.startOfDay(for: startDate)

    while(currentDate <= calendarEndDate) {
        print(currentDate)
        currentDate = Calendar.current.date(byAdding: .day, value: 1, to: currentDate)!
        let dateString = currentDate.convertDateToString(withFormat: "MMM-dd-yyyy h:mm a")
        self.totalDaysFromServerArray.append(dateString)
        print("\(self.totalDaysFromServerArray)")
    }
}

但是,输出是

self.totalDaysFromServerArray is ["Oct-13-2017 12:00 AM", "Oct-14-2017 12:00 AM", "Oct-15-2017 12:00 AM"]

但是,它应该打印出来 10月12日至2017年10月14日至17日

任何人都可以建议我,我的代码出错了,谢谢。

1 个答案:

答案 0 :(得分:0)

如果您直接打印startdateenddate(不使用字符串进行转换),则分别打印2017-10-12 16:00:00 +00002017-10-14 00:00:00 +0000,因为它将位于{{1}格式。当您将该日期转换为字符串时,它将在您当地的时区!例如,如果您在UTC,则您的时区将为indiaGMT + 05:30,因此在转换为字符串时,您的日期会增加UTC + 05:30小时。

所以,你的代码没有任何问题。您的结果日期在05 : 30时区,因为您已将它们转换为字符串。

现在,如果gmt + 05 : 30中有人这样做,那么他/她会在当地时区找到它!我的意思是他们会在您的问题中获得您期望的输出,因为英国的时区为UKUTC + 00 : 00

现在,如果您想在GMT + 00 : 00或任何特定时区显示字符串值,或者如果您想在将日期转换为字符串时阻止上述情况,则可以设置特定时区!然后它会将日期转换为该时区的字符串(参见下面的示例)。但是,除非你几乎不需要它,否则它是不正确的,因为正常行为是你在本地时区向用户显示日期。

例如,

utc

,你的功能将是,

 let value = "1507824000" //2017-10-12 16:00:00 +0000
    let value2 = "1507939200" //2017-10-14 00:00:00 +0000

    let convertedDate = Double(value)

    let convertedEndDate = Double(value2)

    let startdate = Date(timeIntervalSince1970: convertedDate!)


    let enddate = Date(timeIntervalSince1970: convertedEndDate!)
    self.showRange(between: startdate, and: enddate)

你的结果将是,

   func showRange(between startDate: Date, and endDate: Date) {
    guard startDate < endDate else { return }

    let calendar = Calendar.current
    let calendarEndDate = calendar.startOfDay(for: endDate)

    var currentDate = calendar.startOfDay(for: startDate)

    while(currentDate <= calendarEndDate) {
        print(currentDate)
        currentDate = Calendar.current.date(byAdding: .day, value: 1, to: currentDate)!
        let df = DateFormatter.init()
        df.dateFormat = "MMM-dd-yyyy h:mm a"
        df.timeZone = TimeZone.init(abbreviation: "BST")

        let dateString = df.string(from: currentDate)
        totalDaysFromServerArray.append(dateString)

    }
    print("\(totalDaysFromServerArray)")
}

希望这会有所帮助!