swift stored date turns different when formatted to string [duplicate] - swift

This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 7 years ago.
I stored a date in core data (as a date), and with the println it shows correctly its value: april 21 (is the var dateX below), but when right after the println i format it to string with the following code, the label linked to it shows april 22 (which is today, so i wonder tomorrow will show 23 etc.), where is the problem? anyone?
thank you
if dateX != nil{
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy"
dateFormatter.timeZone = NSTimeZone.defaultTimeZone()
var dateXstring = dateFormatter.stringFromDate(dateX as NSDate)
startLabel.text = "Profile created on \(dateXstring)"
}
println dateX and dateXstring:
my time zone is Rome (Italy)

You likely have a timezone issue. Where are you located? DefaultTimeZone could be GMT/ZULU time which is -5 hrs from the east coast.
A good way to check is to use the timeIntervalSince1970 function (i think thats what it is called). If the stored date and retrieved date have the same value its the same date and you have a display problem.
timeIntervalSince1970 returns a NSTimeInterval which is really a Double

Related

Swift 5 - Dateformatter not working as expected [duplicate]

This question already has answers here:
Swift - Get local date and time
(11 answers)
Closed 1 year ago.
I was playing around with date formatter in swift, but the AM/PM thing is not working in my code.
import Foundation
let dtstr = "Tuesday, July 28, 2020 4:15:45 PM"
let formatter = DateFormatter()
formatter.dateFormat = "eeee, MMMM d, yyyy h:m:s a"
let date = formatter.date(from: dtstr)
print(date)
the output is this: Optional(2020-07-28 08:15:45 +0000). However, it should be 16:15:45 instead of this. Any idea why?
Thanks!
Date has no information about time zone, and default string representation is using a greenwich one. You can see it +0000 part in your string.
You can get description for your own time zone like this:
date.description(with: .current)

Date string to Swift Date [duplicate]

This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 1 year ago.
I've tried using a DateFormatter() to format a string (from C# DateTime) into a Swift Date(). However, I keep getting nil.
Here is the code I've been playing around with:
let dateAndTimeString = "2021-08-24T10:16:06.647" //Copied from a C# API
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" //Also trying without this dateFormat returns nil
let date = dateFormatter.date(from: dateAndTimeString) //Returns nil
If anyone could help point me in the right direction, that would be great!
The dateFormat you used is not matched with the given date string. The correct dateFormat should be below
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"

Why does print(date object) show a different date than what the value actually is? [duplicate]

This question already has answers here:
NSDate() or Date() shows the wrong time
(2 answers)
Closed 2 years ago.
I am playing around with DateFormatter in xcode playground to try to learn the basic of how the date object works in swift.
The following code gave my strange results when i tried to print the result from a string to date conversion from this string "050478" (5. April 1978) to a date . In Norway our social security number starts with ddMMyy, so it is that number i want to convert to a date.
import UIKit
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "dd/MM/yy"
let myDateString = "05/04/78"
if let myDate = dateFormatterGet.date(from: myDateString)
{
print(myDate)
}
As you can see, the print(myDate) command gives me the wrong date from the day before what i a gave as argument to the dateformatter (1978-04-04)
The local time zone in Norway on May 5th at 0:00 is UTC+0100.
However print() displays dates always in UTC(+0000) which is May 4th at 23:00.
Replace
print(myDate)
with
print(myDate.description(with: .current))
to get the local date and time

DateFormatter displaying Date Wrongly in iPhone device [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 5 years ago.
I did an extension for Date that returns a formatted string:
extension Date {
var myFormattedDate : String {
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "EEEE, MMMM d, y (HH:mm a)"
return formatter.string(for: self)!
}
}
On runtime, I set a breakpoint inside the myFormattedDate property.
po self printed:
2017-09-05 08:50:00 +0000
po formatter.string(for: self)! printed:
Tuesday, September 5, 2017 (11:50 AM)"
What could be the problem?
Thanks!
Printing a Date always returns an UTC time, regardless of the local time zone. Just avoid printing a Date object directly if you want to see the date with the proper time zone in your console.

Swift language bug in DateFormatter? [duplicate]

This question already has answers here:
1st april dates of 80s failed to parse in iOS 10.0
(1 answer)
Why NSDateFormatter is returning null for a 19/10/2014 in a Brazilian time zone?
(3 answers)
DateFormatter's returns nil for specific date strings without time in Swift
(1 answer)
Closed 5 years ago.
I was testing an app that has an UITextField where users enter a date, so I take the entered string and use a DateFormatter to generate a Date object.
The problem first occurred when I tried converting the string "10/15/2017" which returned a nil value. So I create a code to generate string from "01/01/2000" to "12/31/2020" and I realized that the problem is occurring in all years around October or November of each year.
I created a code to print all dates that return a nil value:
import UIKit
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.locale = Locale(identifier: "en_US")
for day in 1...30 { // Generate the days
for month in 1...12 { // Generate the months
for year in 2000...2020 {
if month == 2, day > 28 { // Check if month is february
continue
}
str = "\(String(format: "%02d", month))/\(String(format: "%02d", day))/\(String(format: "%02d", year))"
let date = dateFormatter.date(from: str)
if date == nil {
print("\(str)")
}
}
}
}
I also tried changing the dateFormat, or even the locale properties and I'm also getting nil for some entries.
dateFormatter.dateFormat = "MM/dd/yyyy"
This snippet of code prints the following:
11/02/2004
11/03/2002
11/05/2006
10/08/2000
10/14/2001
10/14/2007
10/15/2017
10/16/2005
10/16/2011
10/16/2016
10/17/2010
10/18/2009
10/18/2015
10/18/2020
10/19/2003
10/19/2008
10/19/2014
10/20/2013
10/20/2019
10/21/2012
10/21/2018
I'm using Xcode 8.3.3 and Swift 3. Is that a bug with Swift/Xcode or I'm doing something wrong?
It's most likely the Brazil-Fall-Syndrome
The daylight saving changes in Brazil occur at midnight. In fall, when the clock is set forward, there is no 12:00 am / 0:00 so the date is nil.
Set the time zone to a country where daylight saving changes don't occur at midnight or set the hour to noon