Date to String [format string] String to Date in Swift - ios

I've racked my brain over this issue all day and can't seem to get a solution. I have a fitness program that queries the Health Data Store. In the predicateForSamples(withStart: myStartDate, end: myEndDate) I am finding that getting a date from a datePicker sends a moment in time and therefore my query does not return the results for the entire day as I'd like. So, I figured if I take that datePicker date and convert it to a starting and ending format my issue would be solved. However, using the let date = dateFormatter.date(from: dateString) is returning the correct date but the time returns as 04:00:00 +0000 for both functions.
If anybody has a suggestion, please feel free to lend a helping hand. Thank you so very much!
func convertStartDate(StartDate: Date) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyy-MM-dd '00':'00':'01' +0000"
let dateString = dateFormatter.string(from: StartDate)
print("convertingStartDate() - \(dateString)")
let date = dateFormatter.date(from: dateString)
print(date as Any)
return date!
}
func convertEndDate(EndDate: Date) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyy-MM-dd '23':'59':'59' +0000"
let dateString = dateFormatter.string(from: EndDate)
print("convertingEndDate() - \(dateString)")
let date = dateFormatter.date(from: dateString)
return date!
}

This should fix your issue
func convertStartDate(StartDate: Date) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyy-MM-dd '00':'00':'01' +0000"
let dateString = dateFormatter.string(from: StartDate)
dateFormatter.dateFormat = "yyy-MM-dd HH:mm:ss +0000"
let date = dateFormatter.date(from: dateString)
print(date as Any)
return date!
}
func convertEndDate(EndDate: Date) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyy-MM-dd '23':'59':'59' +0000"
let dateString = dateFormatter.string(from: EndDate)
dateFormatter.dateFormat = "yyy-MM-dd HH:mm:ss +0000"
let date = dateFormatter.date(from: dateString)
return date!
}

In addition to above, this is actually fixed by setting dateFormatter.timeZone = TimeZone(secondsFromGMT: 0), as the seconds from GMT is your issue.

Related

Swift datefomatter returns wrong time

I am working in iOS app with Dateformatter which is return wrong time. I don't have any idea to fix this. Can anyone please correct me to get correct time?
let dateString = "2020-08-11T05:32:33.000Z"
func approach1(){
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
guard let date = dateFormatter.date(from: dateString) else {fatalError()}
printTime(date: date)
}
func approach2(){
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
guard let date = dateFormatter.date(from: dateString) else { fatalError()}
printTime(date: date)
}
func printTime(date: Date){
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)
print("date: \(date)")
print("Time: \(time)") // Here it's always wrong time with those approach.
}
Current Result:
By default dateFormatter works with local time zone. To have correct display set its time zone to UTC : dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
There's nothing wrong. The date formatter returns the correct time
The date string
"2020-08-11T05:32:33.000Z"
represents a date in UTC(+0000).
Your local time zone is obviously UTC+0530. DateFormatter considers the local time zone by default.
To format the time also in UTC you have to set the time zone of the date formatter explicitly
func printTime(date: Date) {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)
print("date: \(date)")
print("Time: \(time)")
}
You need to specify the time zone when formatting to string (by default it's using your current local time zone):
func printTime(date: Date) {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC") // <- add here
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)
print("date: \(date)")
print("Time: \(time)")
}

Get month and date separately from date string

I have got a date in this format..
2019-12-16 18:30:00 +0000
This is the code I have for that..
var utcTime = "\(dic["dueDate"]!)"
self.dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
self.dateFormatter.locale = Locale(identifier: "en_US")
let date = self.dateFormatter.date(from:utcTime)!
print(date)
I wanted to extract month and date from this string. i.e. from the above date string, I want 'December' & '16' separately.
There are several ways to get the expected result, as an option you can use this code with Calendar:
let utcTime = "2020-01-17T22:01:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US")
if let date = dateFormatter.date(from:utcTime) {
let monthInt = Calendar.current.component(.month, from: date)
let dayInt = Calendar.current.component(.day, from: date)
let monthStr = Calendar.current.monthSymbols[monthInt-1]
print(monthStr, dayInt)
}
Welcome to stack overflow.
You can try this :
let calendar = Calendar.current
calendar.component(.year, from: date)
calendar.component(.month, from: date)
calendar.component(.day, from: date)
Hope it helps...
Welcome to stack overflow. Please try this.
func getMonthAndDate(dateString: String) ->(month:String , day:String) {
guard let date = Date.getMonthAndDate(from: dateString, with: "yyyy-MM-dd'T'HH:mm:ss") else {
return ("","")
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM"
let month = dateFormatter.string(from: date)
dateFormatter.dateFormat = "dd"
let day = dateFormatter.string(from: date)
return (month,day)
}
extension Date {
static func getMonthAndDate(from str: String, with formatter: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current//(abbreviation: "GMT") //Set timezone that you want
dateFormatter.locale = NSLocale.current
dateFormatter.dateFormat = formatter //Specify your format that you want
return dateFormatter.date(from: str)
}
}
Swift 5
Here is the extension you need It returns tuple having Month and date as you wanted to have
extension Date {
func getMonthAndDate() ->(month:String , day:String) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM"
let month = dateFormatter.string(from: self)
dateFormatter.dateFormat = "dd"
let day = dateFormatter.string(from: self)
return (month,day)
}
}
I give you example of month u can get date and month value separately ,
visit link for your format http://userguide.icu-project.org/formatparse/datetime
extension Date {
var month: String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM"
return dateFormatter.string(from: self)
}
}
you can use it in this way:
let date = Date()
let monthString = date.month
try same thing for date, I hope it will work for you... :)
this is an example from your code. I have stored month and day in separate string to show you. You can change according to your requirements.
var utcTime = "2019-12-16 18:30:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss z"
dateFormatter.locale = Locale(identifier: "en_US")
let date = dateFormatter.date(from:utcTime)!
print(date) //2019-12-16 18:30:00 +0000
dateFormatter.dateFormat = "MMMM"
let strMonth = dateFormatter.string(from: date)
print(strMonth) //December
dateFormatter.dateFormat = "dd"
let strDay = dateFormatter.string(from: date)
print(strDay) //16
Also you can use Calendar object to get date, month (gives you in digit) and year.
var utcTime = "2019-12-16 18:30:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss z"
dateFormatter.locale = Locale(identifier: "en_US")
let date = dateFormatter.date(from:utcTime)!
let calendarDate = Calendar.current.dateComponents([.day, .year, .month], from: date)
let day = calendarDate.day
print(day) //16
let month = calendarDate.month
print(month) //12
let year = calendarDate.year
print(year) //2019
You can get the day, month and year as follows
let yourDate = Calendar.current.dateComponents([.day, .year, .month], from: Date())
if let day = yourDate.day, let month = yourDate.month, let year = yourDate.year {
let monthName = Calendar.current.monthSymbols[month - 1]
// your code here
}
extension String {
func getMonthDay() -> (Int,Int) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"
let date = dateFormatter.date(from: self) ?? Date()
let calendar = Calendar.current
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
return (month, day)
}
}

IOS conversion of UTC to local time not accurate

I'm trying to convert a UTC to device local time in Swift 4. I found many solutions on stackoverflow for that and I implemented them in my code. My code works fine but it doesn't return the right answer.
My code is:
func UTCToLocal() -> String {
let a = "2:36:27 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm:ss aa"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: a)
dateFormatter.timeZone = TimeZone.current
dateFormatter.defaultDate = Date()
dateFormatter.dateFormat = "hh:mm:ss aa"
return dateFormatter.string(from: dt!)
}
According to my local time, Lebanon, this method should return the value 5:36:27 PM. However, it is returning 4:36:27 PM.
Note: I checked my device's local time and it's set correctly
This is due to Daylight Saving Time in Lebanon. You can take the time offset into consideration by using the daylightSavingTimeOffset(for:) method on your time zone:
func UTCToLocal() -> String {
let a = "2:36:27 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm:ss aa"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: a)
dateFormatter.timeZone = TimeZone.current
let offset = dateFormatter.timeZone.daylightSavingTimeOffset(for: dt!)
dateFormatter.defaultDate = Date()
dateFormatter.dateFormat = "hh:mm:ss aa"
return dateFormatter.string(from: dt! + offset)
}
UTCToLocal()
Note that dt is "Jan 1, 2000 at 2:36 PM" since you haven't specified the .day, .month, and .year date components in a

Date from String is not giving correct date

I want to set "EST" as the default time zone for every user, but there is one condition that needs to be check in the current date at 7:45 PM. So I am comparing two dates, but the problem is when I convert the current Date to String it gives me the correct EST time, when I convert that String again to Date in EST it gives me time 4 hours ahead of EST. Here is the code for conversion
class func getCurrentDateTime() -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST")
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateString = dateFormatter.string(from: Date())
print(dateString)
let convertDateFormatter = DateFormatter()
convertDateFormatter.dateFormat = "dd-MM-yyyy"
convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let currentDate = convertDateFormatter.string(from: Date())
print(currentDate)
let comparedDateFormatter = DateFormatter()
comparedDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
comparedDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let comparedDate = comparedDateFormatter.date(from: "\(currentDate) 19:45:00")
print(comparedDate)
let currentDateFormatter = DateFormatter()
currentDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
comparedDateFormatter.timeZone = TimeZone.init(abbreviation: "EST")
let currentDateAndTime = currentDateFormatter.date(from: dateString)
print(currentDateAndTime)
return dateString
}
So a date does not have a time zone.
So when I use a dateFormatter to convert a date to a string representation of the string will reflect the time zone the dateFormatter is set to.
But when you use the same formatter to convert the the string back into a date the date would not have the the time zone offset anymore.
So this sounds to me as if it is working properly.
Edit:
So if you are trying to compare two dates I would do something like:
let date1 = Date()
let date2 = Date().addingTimeInterval(100)
if date1 == date2 {
// dates are exactly equal
} else if date1 > date2 {
// date1 is the most recent
} else if date1 < date2 {
// date2 is the most recent
}
And if I were trying to display these dates I would use the date formatter to convert them to strings.
I modified the code as you requested:
func getCurrentDateTime() -> String {
var checkString : String!
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST")
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss a"
let dateString:String! = dateFormatter.string(from: Date())
let dateFormatter1 = DateFormatter()
dateFormatter1.timeZone = TimeZone(abbreviation: "EST")
dateFormatter1.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateString1:String! = dateFormatter1.string(from: Date())
let convertDateFormatter = DateFormatter()
convertDateFormatter.dateFormat = "dd-MM-yyyy"
convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let currentDateformat = convertDateFormatter.string(from: Date())
let compareDate = "\(currentDateformat) 07:45:00 PM"
let compareDate1 = "\(currentDateformat) 07:45:00"
if dateString == compareDate {
checkString = "equal date"
}
if dateString1 < compareDate1{
checkString = "greater than"
} else {
checkString = "less than"
}
return checkString
}
func getCurrentDateTime() -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST")
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateString = dateFormatter.string(from: Date())
let convertDateFormatter = DateFormatter()
convertDateFormatter.dateFormat = "dd-MM-yyyy"
convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let currentDate = convertDateFormatter.string(from: Date())
let comparedDateFormatter = DateFormatter()
comparedDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let comparedDate = comparedDateFormatter.date(from: "\(currentDate) 19:45:00")
let currentDateFormatter = DateFormatter()
currentDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let currentDateAndTime = currentDateFormatter.date(from: dateString)
return dateString
}
Plesae check this code .
Actually I didn't find anything wrong with the code.
The Date always be in UTC timezone. The DateFormatter did the magic.
To print Date in as per the format:
use string(from:) method.
if let currentDateAndTime = currentDateFormatter.date(from: dateString) {
print(currentDateFormatter.string(from: currentDateAndTime))
}
NB: When working with fixed format dates, such as RFC 3339, you set
the dateFormat property to specify a format string. For most fixed
formats, you should also set the locale property to a POSIX locale
("en_US_POSIX"), and set the timeZone property to UTC.

UTC Date To Current TimeZone String and Result StringDate to Current TimeZone Date

I get the current date in UTC and I converted it into Local TimeZone that result comes in string.
Now, I converted that Result String Date into a Date with the same Local TimeZone but it gives the date in UTC.
Below is the Code I am using:
print("current date :\( Date())") //current date : 2017-03-08 11:11:12 +0000
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss Z"
let localDate : String = dateFormatter.string(from: Date())
print("localDate date :\(localDate)") // localDate date: 08-03-2017 16:41:12 +0530
let dateFormatter1 = DateFormatter()
dateFormatter1.timeZone = TimeZone.current
dateFormatter1.dateFormat = "dd-MM-yyyy HH:mm:ss Z"
let dateCurrent = dateFormatter1.date(from: localDate)!
print("dateCurrent date :\(dateCurrent)") // dateCurrent date: 2017-03-08 11:11:12 +0000
dateCurrent
I want in
08-03-2017 16:41:12 +0530
only of type Date not of type String.
Where have I gone wrong?
Thanks everyone for your Answers,
I dug out and I found the solution. Here I am sharing the code.
let currentDate = Date()
let CurrentTimeZone: TimeZone = TimeZone(abbreviation: "UTC")!
let SystemTimeZone: TimeZone = TimeZone.current
let currentGMTOffset: Int = CurrentTimeZone.secondsFromGMT(for: currentDate)
let SystemGMTOffset: Int = SystemTimeZone.secondsFromGMT(for: currentDate)
let interval: TimeInterval = Double(SystemGMTOffset) - Double(currentGMTOffset)
let todayDate: Date = Date(timeInterval: interval, since: currentDate)
print(" Today Date : \(todayDate)")
This is how Date objects work. You simply keep the date object until you need to output it to the user or an API, at which point you can convert it to a string in the appropriate time zone.
A Date object essentially just wraps a unix timestamp with some helper methods for dealing with it.
try this, Hope it helps
let date: String = "dd-MM-yyyy HH:mm:ss Z"
var dateArr = date.components(separatedBy: " ")
let dateCurrent = (dateArr [0] )
print(dateCurrent)
Issue with your dateFormatter. Here is my code to conver UTC to Local and Local to UTC:
convertUTCtoLocal(strUTCDate: "09-03-2017 03:54:00")
convertLocalToUTC(strUTCDate: "09-03-2017 09:24:00")
Convert UTC to Local
func convertUTCtoLocal(strUTCDate: String){
let dateFormater : DateFormatter = DateFormatter();
dateFormater.timeZone = TimeZone(abbreviation: "UTC")
dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateFromString = dateFormater.date(from: strUTCDate)
dateFormater.timeZone = NSTimeZone.local as TimeZone!
dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"
let localData = dateFormater.string(from: dateFromString!)
print("Your local date is \(localData)")
}
Convert Local to UTC
func convertLocalToUTC(strUTCDate: String){
let dateFormater : DateFormatter = DateFormatter();
dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormater.timeZone = NSTimeZone.local as TimeZone!
let dateFromString = dateFormater.date(from: strUTCDate)
dateFormater.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!;
dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"
let localData = dateFormater.string(from: dateFromString!)
print("Your UTC date is \(localData)")
}

Resources