0

I am getting date in this format "Thu Jul 20 06:44:40 +0000 2017" and I want to convert it in milliseconds so that I can compare this milliseconds to current date milliseconds.

I want to get 20 min difference from this date "Thu Jul 20 06:44:40 +0000 2017" to current date.
I want to check If 20 or less than 20 min difference is there then only I will do other operation.

I don't know how can I check 20 min difference.

//MiliSeconds from Date
func miliSecFromDate(date : String) -> String {
    let strTime = date
    let formatter = DateFormatter()
    formatter.dateFormat = "E MMM d HH:mm:ss Z yyyy"
    let ObjDate = formatter.date(from: strTime)
    return (String(describing: ObjDate!.millisecondsSince1970))
}
Kishor Pahalwani
  • 1,010
  • 1
  • 23
  • 53
  • 1
    Apple's [Data Formatting Guide](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html) has a link to http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns, where you find all necessary information to create the date format appropriate for "Thu Jul 20 06:44:40 +0000 2017". – Martin R Jul 20 '17 at 07:21
  • use this dateFormatter.dateFormat = "EEE MMM DD hh:mm:ss +zzzz yyyy" let elapsed = ObjDate.timeIntervalSince(date!) print(elapsed) – BHAVIK Jul 20 '17 at 07:42

3 Answers3

2

Your date formatter is wrong.

Try using this:

let formatter = DateFormatter()
formatter.dateFormat = "E MMM d HH:mm:ss Z yyyy"

However you SHOULDN'T be comparing dates "in milliseconds". The reason is that, when you convert it the time zone information is lost.

You should just compare Date instances directly since Swift supports it.

Check this answer to know how:

Swift 3 - Comparing Date objects

Pochi
  • 13,391
  • 3
  • 64
  • 104
2

For compare to current time time less than or equal 20

Step 1:

Make Function which convert Given time to milli second Since Current Date and time.

func miliSecFromDate(date : String) -> TimeInterval {
        let strTime = date
        let formatter = DateFormatter()
        formatter.dateFormat = "EEE MMM dd hh:mm:ss +zzzz yyyy"
        let ObjDate = formatter.date(from: strTime)
        return (ObjDate?.timeIntervalSinceNow)!
}

Step 2:

Now check Given time is less than or equal to 20 minute.

if miliSecFromDate(date: "Thu Jul 20 10:42:14 +0000 2017") >= -1200  {
                print("less than 20 minute")
            }else{
                print("condition False")
}
Sakir Sherasiya
  • 1,562
  • 1
  • 17
  • 31
0

Try this code:

 func getMilliseconds(){   
     let strDate = "Thu Jul 20 06:44:40 +0000 2017"
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "EEE MMM DD hh:mm:ss +zzzz yyyy"
            let date = dateFormatter.date(from: strDate)
            print(date!)
            let millieseconds = self.getDiffernce(toTime: date!)
            print(millieseconds)
    }
    func getDiffernce(toTime:Date) -> Int{
        let elapsed = NSDate().timeIntervalSince(toTime)
        return Int(elapsed * 1000)
      }
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20