storing date in coredata swift - 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!)

Related

Swift date components incorrect return of date day number

I need to obtain a date from some variable values
So I specify year, month and day and I need a Date as return
Doing the following works except for the day because it return the day input - 1
let todayDate: Date = Calendar.current.startOfDay(for: Date.from(year: 2022, month: 09, day: 05)!)
print("today date = \(todayDate)")
extension Date {
static func from(year: Int, month: Int, day: Int) -> Date? {
let calendar = Calendar(identifier: .gregorian)
var dateComponents = DateComponents()
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
return calendar.date(from: dateComponents) ?? nil
}
}
And the output is
today date = 2022-09-04 22:00:00 +0000
Date and time can be a bit tricky. The Date struct stores a point in time relative to GMT. If you print it it will show exactly that.
Solution:
Don´t use print, use a proper Dateformatter. To illustrate what I mean use this in a playground:
let date = Calendar.current.startOfDay(for: Date())
print(date)
//2022-09-03 22:00:00 +0000
// when it is 4.th of september 00:00 in my timezone (+- Daylight saving) it is this time in GMT
let formatter = DateFormatter()
formatter.dateFormat = "dd MM yyyy HH:mm:ss"
print(formatter.string(from: date))
//04 09 2022 00:00:00
// this is the time in my timezone
So the issue here is not that it has the wrong time, it is just not presented in the correct time zone.

Swift date manipulation - strange month return

I'm trying to do some date manipulation with Swift and I'm getting an unexpected result. The webservice will pass in a string date, and then I want to get that month and the previous month. I'm using this code (with input grab and such removed):
import Foundation
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(identifier: "America/New_York")
formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.date(from: "2018-12-01")!
let prev = Calendar.current.date(byAdding: .month, value: -1, to: date)!
formatter.string(from: date)
formatter.string(from: prev)
So I've got a valid date, and then I subtract a month from it. The first formatted date shows my expected 2018-12-01 but then on the second line, instead of saying 2018-11-01 it says 2018-10-31.
I'm in PST, which is of course 3 hours ahead of EST. If I add 3 hours I'd get the expected strings. However, since both the input and the output strings were done with a formatter using the timezone, why don't I get the expected output?
The problem is that Calendar.current is in a different timezone (for you) than the formatter.
So date is December 1, 2018 at midnight New York time. But that is November 30, 2018 at 9pm local time (PST) for you.
When you subtract one month it is done in local time (Calendar.current) so you get October 30, 2018 at 9pm. Then you format that date to New York time and it results in October 31, 2018 at midnight.
To get the proper results you want a Calendar in the same timezone as the formatter:
var cal = Calendar(identifier: Calendar.current.identifier)
cal.timeZone = formatter.timeZone
let prev = cal.date(byAdding: .month, value: -1, to: date)!
This will give the expected result.

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

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.

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.

New month date continues to count up

The new month of Feburary date continues to count up from January. So instead of showing Feburary 1St, it's showing Feburary 32 Like the picture below, any help would be appreciated thanks.
This is how I am getting the current date:
let date = Date()
let format = DateFormatter()
format.dateFormat = "EE, MMM DD, YYYY"
let currentDate = format.string(from: date)
header.headerTitle.text = currentDate
This is the result Feburary 32, 2018
Change "EE, MMM DD, YYYY" to "EE, MMM dd, yyyy" (or maybe just one d) and next time please try to read up on how date formatters work before trying to use them:
http://userguide.icu-project.org/formatparse/datetime