Convert a mysql date to NSDate then check how many days between - swift

I'm having problems with NSDate in Swift.
I JSON a date formatted in Y-m-d from my database. This is stored as a string called survey_date
I then convert the string to a NSDate using dateFormatter
I want to work out the amount of days between the survey_date and NOW
Error in my script is endDate is ambiguous expression without more context.
Here is my script:
var survey_date = prefs.valueForKey("SURVEY_DATE") as! String
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var date = dateFormatter.dateFromString(survey_date) as NSDate!
let outputDate = dateFormatter.stringFromDate(date)
println(outputDate)
let start = outputDate
let end = NSDate()
let dateFormatter2 = NSDateFormatter()
dateFormatter2.dateFormat = "yyyy-MM-dd"
let startDate:NSDate = dateFormatter2.dateFromString(start)!
let endDate:NSDate = dateFormatter2.dateFromString(end)!
let cal = NSCalendar.currentCalendar()
let unit:NSCalendarUnit = .CalendarUnitDay
let components = cal.components(unit, fromDate: startDate, toDate: endDate, options: nil)
println(components)
Secondly, I also want to be able to convert the date when possible to a different format - for example d m, Y. This is pretty simple to do in PHP but I can't find a way of doing it in Swift? I don't want to have to send extra data through JSON (i.e. create a $formatted_date) but it looks like this is my only option (which I know is messy and I will get told off for!)

You get a compiler error in
let endDate:NSDate = dateFormatter2.dateFromString(end)!
because dateFromString() is called with a date instead of a string,
which makes no sense.
Your computation is a bit too complicated, you can shorten it to
let survey_date = "2015-05-01"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
if let startDate = dateFormatter.dateFromString(survey_date) {
let endDate = NSDate()
let cal = NSCalendar.currentCalendar()
let components = cal.components(.CalendarUnitDay, fromDate: startDate, toDate: endDate, options: nil)
let days = components.day
println(days)
} else {
println("invalid input")
}
If you want to convert the date into a different format then
(as Leo already said in a comment), use stringFromDate() with
a second date formatter, e.g.
let dateFormatter2 = NSDateFormatter()
dateFormatter2.dateFormat = "d MM, Y"
let formattedStartDate = dateFormatter2.stringFromDate(startDate)
println(formattedStartDate)

Here's something you may find handy. I was working on an app that did a lot of date calculations, and therefore was making lots of calls to NSCalendar's components(_:fromDate:toDate:options:) method. This doesn't feel terribly Swift-like, so I overloaded the - operator to do date subtractions:
func -(lhs: NSDate, rhs: NSDate) -> NSDateComponents
{
let components: NSCalendarUnit =
.CalendarUnitSecond |
.CalendarUnitMinute |
.CalendarUnitHour |
.CalendarUnitDay
return NSCalendar.currentCalendar().components(components,
fromDate: rhs,
toDate: lhs,
options: nil)
}
This lets you find the difference between any two dates/times by simple subtraction. Here's a quick example:
// MySQL date formatter
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
// Let's create a couple of dates that we know are one day apart
let christmas2015 = dateFormatter.dateFromString("2015-12-25")!
let christmasEve2015 = dateFormatter.dateFromString("2015-12-24")!
let difference = christmas2015 - christmasEve2015
let daysBetween = difference.day // this value is 1

Related

Getting Hour value from time string in Swift

I am trying to get an Int value for Hour from a string.
let time = 13:24
func getNtfHour() -> Date{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH"
let result = dateFormatter.date(from: time)!
return result //2000-01-01 08:24:00 +0000
}
As you can see my current code return a whole date and the time does not match with the string. How do I fix it?
edit: I managed to to this by using date components
let time = 13:24
func getNtfHour() -> Int{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let result = dateFormatter.date(from: time)!
let calendar = Calendar.current.component(.hour, from: result)
return calendar //13
}
As a time string is actually not related to a Dateformatter an easier way is to parse the string directly.
If the hour has always two digits
let time = "13:24"
let hour = Int(time.prefix(2))
or otherwise
let time = "13:24"
if let range = time.range(of: "^\\d+", options: .regularExpression) {
let hour = Int(time[range])
}

Compare two date - Swift

How can I convert string like (2019-11-02) without time to date format and get current Date device without time then compare with together?
As well as the above using string representations of date, you can actually work with just the dates themselves. Just converting a the string will give you a "start of day" date. The Calendar has a method which will do the same with a date, allowing you to compare the converted string to 'today'
func isDateToday(dateString: String) -> Bool {
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
let date = df.date(from: dateString)
let today = Calendar.current.startOfDay(for: Date())
return date == today
}
You can convert the string to date using a dateFormatter then compare with the current date using an if statement
import Foundation
//convert string to date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let myDate = dateFormatter.date(from: "2019-11-02")
//convert today's date in the same formate
let currentFormatter = DateFormatter()
currentFormatter.dateStyle = .short
currentFormatter.dateFormat = "yyyy-MM-dd"
let today = currentFormatter.string(from: Date())
let todayDate = dateFormatter.date(from: today)
//Compare the two date's
if myDate == todayDate {
print("ok")
}

How to extract date and time from standard datetime format?

I am getting a string from API containing date and time as:
createdAt = "2019-08-12T10:34:05.000Z"
I need to extract date from this string and time also.
And how can I get difference as time between two given date strings?
e.g. If I have two strings as:
StartedOn = "2019-08-12T10:32:18.000Z"
StopedOn = "2019-08-12T10:34:05.000Z".
How to get time from start to stop.
swift 4, Xcode 10
Use DateFormatter to get the Date instance from the String, i.e.
let str = "2019-08-12T10:34:05.000Z"
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = formatter.date(from: str)
Now, to get the time difference use timeIntervalSince(_:) with the 2 Date objects, i.e.
let startedOn = "2019-08-12T10:32:18.000Z"
let stoppedOn = "2019-08-12T10:34:05.000Z"
if let startedOnDate = formatter.date(from: startedOn), let stoppedOnDate = formatter.date(from: stoppedOn) {
let timeInterval = stoppedOnDate.timeIntervalSince(startedOnDate)
print(timeInterval)
}
In case you need more detailed components between 2 Date objects, use Calendar object like so,
if let startedOnDate = formatter.date(from: startedOn), let stoppedOnDate = formatter.date(from: stoppedOn) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.hour, .minute, .second], from: startedOnDate, to: stoppedOnDate)
print(components) //hour: 0 minute: 1 second: 47
}
Edit:
To get the date and time separately from Date object,
let str = "2019-08-12T10:34:05.000Z"
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
if let date = formatter.date(from: str) {
formatter.dateFormat = "hh:mm:ss a"
let timeStr = formatter.string(from: date)
print(timeStr) //add timeStr to your timeLabel here...
formatter.dateFormat = "yyyy-MM-dd"
let dateStr = formatter.string(from: date)
print(dateStr) //add dateStr to your dateLabel here...
}
Try with this :D
import datetime
StartedOn = "2019-08-12T10:32:18.000Z"
StopedOn = "2019-08-12T10:34:05.000Z"
StartedOn = datetime.datetime.fromisoformat(StartedOn[:-1])
StoppedOn = datetime.datetime.fromisoformat(StopedOn[:-1])
print(StoppedOn - StartedOn)
There are two dedicated date formatters for that purpose, ISO8601DateFormatter to convert the ISO8601 string to Date
let isoFormatter = ISO8601DateFormatter()
isoFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let createdAtDate = isoFormatter.date(from: "2019-08-12T10:34:05.000Z")
let startedOn = "2019-08-12T10:32:18.000Z"
let stopedOn = "2019-08-12T10:34:05.000Z"
let startedOnDate = isoFormatter.date(from: startedOn)!
let stopedOnDate = isoFormatter.date(from: stopedOn)!
and DateComponentsFormatter to get a m:ss string from the difference between start and end date
let componentsFormatter = DateComponentsFormatter()
componentsFormatter.allowedUnits = [.minute, .second]
let duration = componentsFormatter.string(from: startedOnDate, to: stopedOnDate)

Swift displaying the time or date based on timestamp

I have an API that returns data including a timestamp for that record.
In swift I have the timestamp element loaded and converted into a double and can then convert that into time. I want to be able to return the time if the date of the record is today and return the date if the record is not today.
See below:
let unixTimeString:Double = Double(rowData["Timestamp"] as! String)!
let date = NSDate(timeIntervalSince1970: unixTimeString) // This is on EST time and has not yet been localised.
var dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = .ShortStyle
dateFormatter.doesRelativeDateFormatting = true
// If the date is today then just display the time, if the date is not today display the date and change the text color to grey.
var stringTimestampResponse = dateFormatter.stringFromDate(date)
cell.timestampLabel.text = String(stringTimestampResponse)
Do I use NSCalendar to see if 'date' is today and then do something?
How do you then localise the time so that its correct for the user rather than server time?
There is a handy function on NSCalendar that tells you whether an NSDate is in today or not (requires at least iOS 8) isDateInToday()
To see it working, put this into a playground:
// Create a couple of unix dates.
let timeIntervalToday: NSTimeInterval = NSDate().timeIntervalSince1970
let timeIntervalLastYear: NSTimeInterval = 1438435830
// This is just to show what the dates are.
let now = NSDate(timeIntervalSince1970: timeIntervalToday)
let then = NSDate(timeIntervalSince1970: timeIntervalLastYear)
// This is the function to show a formatted date from the timestamp
func displayTimestamp(ts: Double) -> String {
let date = NSDate(timeIntervalSince1970: ts)
let formatter = NSDateFormatter()
formatter.timeZone = NSTimeZone.systemTimeZone()
if NSCalendar.currentCalendar().isDateInToday(date) {
formatter.dateStyle = .NoStyle
formatter.timeStyle = .ShortStyle
} else {
formatter.dateStyle = .ShortStyle
formatter.timeStyle = .NoStyle
}
return formatter.stringFromDate(date)
}
// This should just show the time.
displayTimestamp(timeIntervalToday)
// This should just show the date.
displayTimestamp(timeIntervalLastYear)
Or, if you just want to see what it looks like without running it yourself:
Abizern's answer in Swift 3:
import UIKit
let timeIntervalToday: TimeInterval = Date().timeIntervalSince1970
let timeIntervalLastYear: TimeInterval = 1438435830
// This is just to show what the dates are.
let now = Date(timeIntervalSince1970: timeIntervalToday)
let then = Date(timeIntervalSince1970: timeIntervalLastYear)
// This is the function to show a formatted date from the timestamp
func displayTimestamp(ts: Double) -> String {
let date = Date(timeIntervalSince1970: ts)
let formatter = DateFormatter()
//formatter.timeZone = NSTimeZone.system
if Calendar.current.isDateInToday(date) {
formatter.dateStyle = .none
formatter.timeStyle = .short
} else {
formatter.dateStyle = .short
formatter.timeStyle = .none
}
return formatter.string(from: date)
}
// This should just show the time.
displayTimestamp(ts: timeIntervalToday)
// This should just show the date.
displayTimestamp(ts: timeIntervalLastYear)

Convert long month name to int

I understand how to use an NSDateFormatter to convert a month component into a long name string, but how does one convert a month name to an int?
I've been using a switch statement to convert, but I'm thinking there must be a simpler way.
For example, I'd like to convert "May" to 5.
You can use DateFormatter custom format "LLLL" to parse your date string (Month). If you are only parsing dates in english you should set the date formatter locale to "en_US_POSIX":
let df = DateFormatter()
df.locale = Locale(identifier: "en_US_POSIX")
df.dateFormat = "LLLL" // if you need 3 letter month just use "LLL"
if let date = df.date(from: "May") {
let month = Calendar.current.component(.month, from: date)
print(month) // 5
}
Thanks Josh. I've converted the Obj-C code and posted it below for future reference:
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
let components = NSDateComponents()
let formatter = NSDateFormatter()
formatter.dateFormat = "MMMM"
let aDate = formatter.dateFromString("May")
let components1 = calendar!.components(.CalendarUnitMonth , fromDate: aDate!)
let monthInt = components.month
Use MM for the month format. Use stringFromDate to convert your NSDate to a String. Then convert your string to an Int with .toInt()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM"
let monthString = dateFormatter.stringFromDate(NSDate()) // "05"
let monthInt = monthString.toInt() // 5
NSDateFormatter has monthSymbols property. Try:
let formatter = NSDateFormatter()
formatter.calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
let monthString = "September"
let month = find(formatter.monthSymbols as! [String], monthString).map { $0 + 1 }
// -> Optional(9)
let monthString2 = "Foobar"
let month2 = find(formatter.monthSymbols as! [String], monthString2).map { $0 + 1 }
// -> nil