How to compare 2 dates and get how far they are apart from each other - ios

I have this string date value
let stringDate = 2018-06-10T13:57:23.232+0000
the string date format will always be the same and I want to have output similar to one of these
6 year, 3 month, 1 day
or
3 month, 20 days
or
1 day
Basically I'm comparing the stringDate with current date and display how far they are apart from each other.
I'm able to get the date from string using this
let dateFormatter = DateFormatter()
var dateFromString: Date?
dateFormatter.timeZone = NSTimeZone.local
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
if let dateReturn = dateFormatter.date(from: stringDate) {
dateFromString = dateReturn
}
but I'm stuck on how to get the custom input like above. Thank you for helping me

Related

Swift date format returning wrong date

I need to convert my date to string and then string to date. date is "2020-10-17 1:22:01 PM +0000"
Here is my date to string conversion code:
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX"
let string = formatter.string(from: "2020-10-17 1:22:01 PM +0000")
let createdAT = string
its returning "2020-10-17 18:51:30+05:30"
Here is my string to date conversion code:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ssZ"
let date = dateFormatter.date(from:date)!
its returning "2020-10-17 1:21:30 PM +0000 - timeIntervalSinceReferenceDate : 624633690.0"
its returning the wrong date after i convert string to date. i need "2020-10-17 18:51:30+05:30" this time to be return when i convert string to date.....
The code in your question is muddled up. You try to convert a string into a string in the first example and something unspecified into a Date in the second example.
Here's how to convert a Date into a String:
import Foundation
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX"
let string: String = formatter.string(from: Date())
print(string) // prints for example 2020-10-18T10:54:07+01:00
Here's how to convert a string into a date
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ssZ"
let date: Date = formatter.date(from: "2020-10-18 10:59:56+0100")! // In real life, handle the optional properly
print(date) // prints 2020-10-18 09:59:56 +0000
When you print a Date directly, it automatically uses UTC as the time zone. This is why it changed it in the code above.
In the examples, I explicitly specified the type of string and date to show what type they are. Type inference means you can omit these in normal code.
As a general rule when handling dates:
always use Date in your code. Date is a type that stores the number of seconds since Jan 1st 1970 UTC.
Only convert dates to strings when displaying them to the user or communicating with an external system.
When calculating periods etc, always use a Calendar to get things like date components and intervals in units other than seconds. You might think to get "the same time tomorrow" you could just add 24 * 60 * 60 to a Date but in many countries, like mine, that will work on only 363 days in the year. Calendar will correctly handle things like daylight saving and leap years.

How to change back year that change into 2000 after using date format

hi I want to get current hour and minute from Date(), so I need to format it into string and want to bring back into date again. But after I try to convert to date the year change into 2000, how can I got back to current year.
//date formatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.timeZone = TimeZone.current
// Get current time and format it to compare
var currentTime = Date() //Get current time
let currentTimeStr = dateFormatter.string(from: currentTime) //get current time only hour and minute
currentTime = dateFormatter.date(from: currentTimeStr)! //this is where the problem because the year change into 1 January 2000
From what I read in the comments, I think you want both the current time in a Date object and also a string with only hours and minutes in "HH:MM" format.
The problem comes from trying to use a formatter that doesn't have a year specified. You are overwriting the currentTime from a string that doesn't have a year (or day, or month) defined, so it defaults to Jan 1st 2000 (the hours and minutes should be correct).
You're also saying you need to format it into a String, and then go back to a Date object. You don't, you already have all the data you need in the Date object, so keep it around and use it when you need to. If this means creating a bunch of DateFormatters all over your project, you can always extend Date to have a function or variable that returns the string with the format you want.
extension Date {
var hoursAndMinutesString: String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.timeZone = TimeZone.current
return dateFormatter.string(from: self)
}
}
Then just call the function from the Date object when you need to, like this:
currentTime.hoursAndMinutesString

dateFromString() returning wrong date swift 3.0

i am passing "01/12/2017" in the fromDate.text(textfield), but receiving unexpected output.
let formatter = DateFormatter.init()
formatter.dateFormat = "dd/mm/yyyy"
startDate = formatter.date(from: fromDate.text!)
print("startDate = \(startDate)")
output is : 31/12/2016
The format of date should be dd/MM/yyyy not dd/mm/yyyy. The mm indicates the minutes and MM indicates the month.
And also add the below line in your code
formatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
This line of code set time zone. If you not, then you get 30/11/2017 in output.
The reason behind this is when string date not contain time then formatter assume that it is midnight and you also not given the timezone so it will take current timezone.
It has to be dd/MM/yyyy dateformat. MM in capital.
func convertToString(of dateTo: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy" //Your New Date format as per requirement change it own
let newDate: String = dateFormatter.string(from: dateTo) //pass Date here
print(newDate) //New formatted Date string
return newDate
}

NSDateFormatter always returning nil

This is my code:
var sdArr = sd.componentsSeparatedByString(".")
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ss"
let date = dateFormatter.dateFromString(sdArr[0])
The value of sdArr[0] is "2015-10-01T14:30:00"
Why is the value of date is nil? This was working fine 2 days ago and I didn't touch my code. Now suddenly out of the blue value of date is nil.
I've tried putting the date string in place of sdArr[0] but it still not working
Please use date formatter as
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
You are using hh which is for 12 hour format, HH is for 24 hour format
add dateformat yyyy-MM-dd'T'hh:mm:ss to yyyy-MM-dd'T'HH:mm:ss because you are getting hour in 24 hour format not 12 hour format so you have to use HH
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" //HH
Please refer Formatting date and time with iPhone SDK? (Vanya's answer) to know more regarding date formaters.
Use this NSDateFormatter
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

Is it possible to add custom text in NSDateFormatter's format string?

Suppose I want the date to look like this:
|1988|December|30|
How can add these to the dateFormatter or for that matter let the format be something like this:
30 in the month of December in the year of 1998
now for 1988,December and 30, I would like to use standard formats but I want the text I put to also accompany them.
specially in case of the above one where, in the format and pipes just come adjacent to the date format for date or month where there is no space between the format and the pipe.
Is any of this possible by just setting the format ?
You can insert arbitrary text (enclosed in single quotes) in the date format, for example.
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:#"dd' in the month of 'MMMM' in the year of 'yyyy"];
NSString *s = [fmt stringFromDate:[NSDate date]];
Result:
09 in the month of July in the year of 2013
Swift Version:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE' text here 'h:mm' and there 'a"
Updated for iOS 13, Swift 5, Xcode 11 and building on Martin R's answer
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "dd' in the month of 'MMMM' in the year of 'yyyy"
let stringDate = dateFormatter.string(from: Date())
print(stringDate)
// printed:
// 7 in the month of October in the year of 2019
P.S. If you wanted an apostrophe, then use: '' directly inside the string. For example "MMM d, ''yy" -> Nov 10, '19
Extension:
If you wanted to add ordinal indicators too (ex. the 'th' after '13th'), you can actually do it inside the date formatter string.
So if you wanted Nov 10th, the code would be:
/// Get date.
let date = Date()
/// Get just the day of the date.
let dayAsInt = Calendar.current.component(.day, from: date)
/// Init the formatter.
let dateFormatter = DateFormatter()
/// Set the format string.
/// Notice we include the 'MMM' to extract the month from the date, but we use a variable to get the 'th' part.
dateFormatter.dateFormat = "MMM '\(dayAsInt.getStringWithOrdinalIndicatorIfPossible)'"
let formattedDate = dateFormatter.string(from: date)
/// Will print out Nov 10th or Apr 1st or whatever.
Here's the extension I made to help:
/// This variable only adds the ordinal indicator if the Int that is calling this function can be converted to an NSNumber.
/// An ordinal indicator is the `rd` after `3rd` or the `st` after `1st`.
var getStringWithOrdinalIndicatorIfPossible: String {
let formatter = NumberFormatter()
formatter.numberStyle = .ordinal
return formatter.string(from: NSNumber(value: self)) ?? "\(self)"
}

Resources