How to format date with DateFormatter that includes friendly day and month - swift

How can I format a date that includes a friendly day and month literal such as:
"Thursday, June 14, 2018"
Day can be:
Sunday, Monday, Tuesday, Wedenesday, Thursday, Friday, Saturday,
Friday
Month can be:
January, February, March, April, May, June, July, August, September,
October, November, December
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.date(from: "Thursday, June 14, 2018")
What should the format be set to?
Something like this?
static let format = "DAY, MONTH, dd, YYYY"
Is it even possible to do this with DateFormatter() ?

Use EEEE for the full weekday name and MMMM for the full month name. But since you are parsing fixed formatted strings that are in English, you must also set the formatter's locale to en_US_POSIX.
let formatter = DateFormatter()
formatter.dateFormat = "EEEE, MMMM d, yyyy"
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter.date(from: "Thursday, June 14, 2018")
Note that this will treat the date as being in the user's local timezone.
See the full specification for all possible date formatting patterns.

Related

Swift Convert date string to Date

I got a date string from server side, which is Tue, 28 May 2019 13:24:06 +0000. I tried to do following:
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
let date = dateFormatter.date(from: "Tue, 28 May 2019 13:24:06 +0000")
result is nil
How can I convert a string like this to a Date?
You need to set a specific dateFormat. And when doing so, set the date formatter's locale to en_US_POSIX.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
let date = dateFormatter.date(from: "Tue, 28 May 2019 13:24:06 +0000")
See the documentation for dateFormat for links that take you a full description of what all of the format specifiers mean.
Keep in mind that when converting a Date to a String for display to the user, then using date and time styles is the best solution. But for parsing a String into a Date, use dateFormat.

How to convert milliseconds to local day, date and time format in swift?

I want to display the date in this format (Wed Jan 10 2018 11:20:17). How to convert milliseconds to this format in swift?I want to get the day as Wed, time as 10:30 AM or PM and the date as 10 Jan.
First convert it in date by dividing it by 1000
var date = Date(timeIntervalSince1970: (1477593000000 / 1000.0))
then use DateFormatter to convert in desired format you need
Note: Not tested in XCODE
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss"
print(dateFormatter.string(from: date))
Hope it is helpful to you.

Using DateFormatter to return iOS lock screen date style

There seem to be five date styles for the DateFormatter class: none, short, medium, long, and full. However, none of these seem to return the lock screen date style, which is as follows:
Tuesday, 6 June
Using DateFormatter's .long style returns the year as well:
Tuesday, 6 June 2017
Additionally, this lock screen date style will change with the current localization/regional settings.
Is there a way to return the date, à la iOS lock screen date style (along with localizational changes)?
You can get the localized format for any combination of date components:
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("EEEE MMMM d")
print(dateFormatter.string(from: date)) // Friday, 4 August
In Spanish:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "es")
dateFormatter.setLocalizedDateFormatFromTemplate("EEEE MMMM d")
print(dateFormatter.string(from: date)) // viernes, 4 de agosto
Note how the order of components has been automatically changed and correct separators inserted.
You can use the custom date format "EEEE, d MMMM"
Swift 3
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, d MMMM"
dateFormatter.string(from: date)
// Friday, 4 August

storing date in coredata swift

I have a textfield connected to a date picker
I am then trying to store the selected date into core data,
My log off the date picked by the user seems ok:
2016-01-29 00:00:00 +0000 [I strip the time component with some code]
This is converted into a String and displayed in the textfield called startDate.
func handleDatePicker(sender: UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM YYYY"
startDate.text = dateFormatter.stringFromDate(sender.date)
}
Now the strange thing is that when I try and store this into CoreData and convert the string back into a date (the attribute I am saving it into is configured as a Date)
let cont = self.context
let newCustomer = NSEntityDescription.entityForName("Customer", inManagedObjectContext: cont)
let aCust = Customer(entity: newCustomer!, insertIntoManagedObjectContext: cont)
let DF = NSDateFormatter()
DF.dateFormat = "EEE, dd MMM YYYY"
aCust.c15das = DF.dateFromString(startDate.text!)
print("Saved Date: \(DF.dateFromString(startDate.text!))")
Now the log prints out:
2015-12-25 00:00:00 +0000
Why the difference? How can I stop this happening?
Sorry if its something obvious that I am not spotting.
"EEE, dd MMM YYYY" -> YYYY: "Week of Year Calendar", aka "ISO Week Date System". The first week does not start on the first January. If the January 1st is either Monday, Tuesday, Wednesday or Thursday, the whole week is the first week of the new year. if it is Friday, Saturday or Sunday the week is the 53rd week of the last year. So this is a calendar with year that only have integral weeks. Either 52 or 53, rather than 365 or 366 days.
In this calendar January 29th would be the 5th day of the 4th week of the year 2016 — 2016-W4-5. This system does not know months and therefor your date is nonsense.
You want "EEE, dd MMM yyyy", as yyyy indicates a year that starts on 1st of January and ends after 31st of December — The Gregorian Year.
[I strip the time component with some code]
You shouldn't do that. Rather NSCalendar's method to get a date at the beginning of the day.
var today: NSDate?
cal.rangeOfUnit(.Day, startDate: &today, interval: nil, forDate: date)
Try this code, that worked for me:
let dateString = "Fri, 29 Jan 2016" // change to your date format
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString(dateString)
print(date!)

dateFromString() returns incorrect date

I'm trying to convert string to Date, but it result incorrect date.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM YYYY"
let dt = dateFormatter.dateFromString("17 Sep 2015")
println("Date : \(dt)")
It result
Date : Optional(2014-12-20 18:30:00 +0000)
Please let me know where I'm making mistake. I tried other format too, but it return nil.
The format for year is incorrect, it should be yyyy, not YYYY.
"Y": Year (in "Week of Year" based calendars). This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.
See: Date Field SymbolTable.
Also: ICU Formatting Dates and Times
Your date format string is wrong. Change it to the following:
dateFormatter.dateFormat = "dd MMM yyyy"
For more information read Date Formatters documentation.