2017-10-15 105 views
1

我想创建一个将日期转换成文本格式,如“刚才,2分钟,1小时,1天,10月10日”的功能。这里就是我在得到一个错误,我的示例代码:如何将日期“MM-dd-yyyy hh:mm:ss a”转换为文字格式?

let components = cal.components([.Day,.Hour,.Minute], fromDate: date, toDate: NSDate(), options:[]) 

这里是我的全码:

func getTextToDisplayFormattingDate(date: NSDate) -> String { 
    var textToDisplay = "" 
    let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "MM-dd-yyyy hh:mm:ss a" 
    dateFormatter.timeZone = TimeZone(identifier: "UTC") 

    let cal = NSCalendar.current 

    let components = cal.components([.Day,.Hour,.Minute], fromDate: date, toDate: NSDate(), options:[]) 

    switch components.day { 
    case 0: 
     if components.hour == 0 { 
      if components.minute <= 0 { 
       textToDisplay = "just now " 
      } else { 
       textToDisplay = "\(components.minute) min" 
      } 
     } else { 
      textToDisplay = "\(components.hours) hrs" 
     } 
    case 1...6: 
     textToDisplay = "\(components.day) d" 
    default: 
     dateFormatter.dateFormat = "MMM dd" 
     textToDisplay = dateFormatter.string(from: date as Date) 
    } 

    return textToDisplay 
} 

error

+0

从你的斯威夫特3码摆脱所有的斯威夫特2语法的。 – rmaddy

回答

0

其实,这是难以计算的日,时,分日期由NSCalendar.components 2之间,你的程序会导致意想不到的结果。通常,我们通过它们之间的TimeInterval进行计算。在SWIFT 3.2或SWIFT 4.0试试这个:

func getTextToDisplayFormattingDate(date: Date) -> String { 
    var textToDisplay = "" 

    let now = Date() 
    let timeInterval = now.timeIntervalSince(date) 
    if timeInterval < 60 { 
     textToDisplay = "just now " 
    } 
    else if timeInterval < 60 * 60 { 
     textToDisplay = "\(Int(timeInterval/60)) min" 
    } 
    else if timeInterval < 60 * 60 * 24 { 
     textToDisplay = "\(Int(timeInterval/60/60)) hrs" 
    } 
    else { 
     let dateFormatter = DateFormatter() 
     dateFormatter.timeZone = TimeZone(identifier: "UTC") 

     if timeInterval < 60 * 60 * 24 * 6 { //less than 6 days 
      // For getting days 
      // textToDisplay = "\(Int(timeInterval/60/60/24)) d" 

      // For getting weekday name 
      dateFormatter.dateFormat = "EEEE" 
      textToDisplay = dateFormatter.string(from: date) //weekday name 
     } 
     else{ 
      dateFormatter.dateFormat = "MMM dd" 
      textToDisplay = dateFormatter.string(from: date as Date) 
     } 
    } 

    return textToDisplay 
} 

记住使用常量来替换表示喜欢60 * 60 * 24以提高性能。

+0

如果不到6天,我该如何显示周日名称。例如:刚才小时,周四,周三,周二,oct'10,oct'9 @yun陈 –

+0

@WhenKey,我更新的,如果不超过6日获得工作日名称的答案。尝试一下。 –

2

使用这样的:

的变化在我的代码:

  1. 在地方NSDate,我用Date
  2. 在地方NSCalendar我用Calendar
  3. components(_:from:to:options:)已更名为dateComponents(_:from:to:)
  4. components值一样day, minute, houroptional。这就是为什么我添加检查之前切换。

    func getTextToDisplayFormattingDate(date: Date) -> String { 
        var textToDisplay = "" 
        let dateFormatter = DateFormatter() 
        dateFormatter.dateFormat = "MM-dd-yyyy hh:mm:ss a" 
        dateFormatter.timeZone = TimeZone(identifier: "UTC") 
    
        let cal = Calendar.current 
        let components = cal.dateComponents([.day, .hour , .minute], from: date, to: Date()) 
    
        if let day = components.day, let minute = components.minute, let hour = components.hour { 
         switch day { 
         case 0: 
          if hour == 0 { 
           if minute <= 0 { 
            textToDisplay = "just now " 
           } else { 
            textToDisplay = "\(minute) min" 
           } 
          } else { 
           textToDisplay = "\(hour) hrs" 
          } 
         case 1...6: 
          textToDisplay = "\(day) d" 
         default: 
          dateFormatter.dateFormat = "MMM dd" 
          textToDisplay = dateFormatter.string(from: date) 
         } 
        } 
        return textToDisplay 
    } 
    
+0

您不需要将日期转换为日期,它已经是日期。 – rmaddy

+0

鉴于问题的性质,它很可能是有益的,如果你增加了一个描述,你的答案是指出,所有你需要做和为什么修复。 – rmaddy

+0

@ViniApp非常感谢!它真的很棒。赞赏 –

相关问题