Convert Etc/GMT time into Swift Date() global TIme - swift

I am trying to convert date from apple receipt to swift global date and time. But I am not succeeded,
Purchased time (device): 2019-08-30 22:23:44 America/Los_Angeles
Purchased time (server): 2019-08-31 05:23:44 Etc/GMT
Expire time (server): 2019-08-31 05:28:44 Etc/GMT
When I get date by using Date(), it's completely or almost some hour difference than my device & those date&time above. I wanted to compare today Date() into expire time.
I used below codes but not success,
extension Date {
static func fromAppleServer(dateString: String) -> Date? {
let seperated = dateString.components(separatedBy: " ")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: seperated.last!)
let dateObject = dateFormatter.date(from: (seperated.dropLast()).joined(separator: " "))
return dateObject
}
static func localUTC() -> Date {
let date = Date()
print("Date before conversion: \(date)")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let string = dateFormatter.string(from: date)
dateFormatter.timeZone = TimeZone(identifier: "America/Los_Angeles")
return dateFormatter.date(from: string)!
}
}
let appleDate = Date.fromAppleServer(dateString: "2019-08-30 22:29:02 America/Los_Angeles") //used 2019-08-31 05:23:44 Etc/GMT too
let localUTC = Date.localUTC()
print("Apple Server: ",appleDate) //Always 2019-08-31 05:29:02 +0000
print("Local UTC: ",localUTC)
Update:
Tried code:
extension Date{
static func fromAppleServer(dateString: String) -> Date?{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
let dateObject = dateFormatter.date(from: dateString)
return dateObject
}
static func localUTC() -> Date{
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
let string = dateFormatter.string(from: date)
dateFormatter.timeZone = TimeZone(identifier: "UTC")
return dateFormatter.date(from: string)!
}
}
Debugging Log:
(lldb) po print date
▿ 2019-08-31 05:28:44 +0000
- timeIntervalSinceReferenceDate : 588922124.0
Fix-it applied, fixed expression was:
print; date
(lldb) po print Date.localUTC()
▿ 2019-09-01 03:07:45 +0000
- timeIntervalSinceReferenceDate : 589000065.0
Fix-it applied, fixed expression was:
print; Date.localUTC()
(lldb) po print self.expiryDate
▿ Optional<String>
- some : "2019-08-31 05:28:44 Etc/GMT"
Fix-it applied, fixed expression was:
print; self.expiryDate
(lldb) po print Date()
▿ 2019-09-01 03:09:03 +0000
- timeIntervalSinceReferenceDate : 589000143.939402
Fix-it applied, fixed expression was:
print; Date()
(lldb)
Here,
Device date is ~ 8:mm PM, Sat 31 August.
Purchased date is(5 mins duration):2019-08-31 05:23:44 Etc/GMT (For device it's 8:mm PM Sat 31 August)
Expire date is: 2019-08-31 05:28:44 Etc/GMT
Swift Date is:~ 2019-09-01 03:09:03
I thought we can easily compare date once Etc/GMT time is converted into Swift Date() global time. But I can't convert 2019-08-31 05:28:44 Etc/GMT to ~ 2019-09-01 03:09:03. So, how to convert it? or Any other suggestions welcome.
Note: '~' indicates some seconds or minutes delay because of testing time.

No need to manually parse your date string. You can use date format "VV" to interprete the timezone identifier associated with your date string:
extension Formatter {
static let customDate: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
return formatter
}()
}
let dateString = "2019-08-31 05:23:44 Etc/GMT" // or "2019-08-30 22:23:44 America/Los_Angeles"
if let date = Formatter.customDate.date(from: dateString) {
print(date) // "2019-08-31 05:23:44 +0000\n"
print(date.description(with: .current)) // "Saturday, August 31, 2019 at 2:28:44 AM Brasilia Standard Time\n"
}

Related

Swift String value converted into strange Date value [duplicate]

This question already has answers here:
What is happening to the year?
(1 answer)
Difference between 'YYYY' and 'yyyy' in NSDateFormatter
(4 answers)
Closed 1 year ago.
extension Date {
func toString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, Y (E)"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
return dateFormatter.string(from: self)
}
}
extension String {
func toDate() -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, Y (E)"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
return dateFormatter.date(from: self)!
}
}
That is my code.
I want to convert my string value into a right date value. The results i’m getting are totally different form what i want.
Here is an example:
String : September 09, 2021 (Thu)
Converted Date : 2020-12-24 00:00:00 +0000
I want it to be like this:
Converted Date : September 09, 2021 (Thu) (or 2021-09-09 00:00:00 +0000)
Can you change
dateFormatter.dateFormat = "MMMM dd, Y (E)"
To:
dateFormatter.dateFormat = "MMMM dd, yyyy (E)"
You pass the full year in String: September 09, 2021 (Thu) so you need to get yyyy to the right way.
extension String {
func toDate() -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy (E)"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
return dateFormatter.date(from: self)!
}
}
here you can do what you want:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd,yyyy (E)" // here is your foramt
dateFormatter.timeZone = TimeZone(identifier: "UTC")
guard let date = dateFormatter.date(from: "September 09, 2021 (Thu)") else {
fatalError()
}
print(date) // output: 2021-09-09 00:00:00 +0000
Note that, the time zone which you choosed, should matched to your device time zone.
Replace Your Function with Following Code
extension Date {
func toString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy (E)"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
return dateFormatter.string(from: self)
}
}
extension String {
func toDate() -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd,yyyy (E)"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
return dateFormatter.date(from: self)!
}
}
There was slight variance of in your dateFormat in your toDate() Function
Replaced MMM from MMMM
Replaced yyyy from Y
I don't know why it was showing different date but now it should work😅

Swift: startOfDay function returning 5:00 AM

I am calling the startOfDay function on the current date but I am getting 05:00 instead of 00:00. Any input on why this is happening would be appreciated.
Code:
let date = Calendar.current.startOfDay(for: Date())
print(date)
output:
2020-08-05 05:00:00 +0000
That's because you're printing Date object directly it considers the time-zone. You can provide a date formatter to convert it.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let date = Calendar.current.startOfDay(for: Date())
let string = dateFormatter.string(from: date)
print(string)

Date not convert in AM/PM Format in swift

I need to date and time with my own format like time in AM/PM (12 hours).
extension Date {
func getStringFromDateWithUTCFormat() -> ObjTimeStamp {
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.timeZone = TimeZone.init(abbreviation: "UTC")
// Get Date
dateFormatterPrint.dateFormat = "MM/dd/yyyy" //"MMM dd,yyyy"
let date = dateFormatterPrint.string(from: self)
// Get Time
dateFormatterPrint.dateFormat = "hh:mm:ss a"
let time = dateFormatterPrint.string(from: self)
return ObjTimeStamp.init(date: date, time: time, timeStamp: self)
}
}
Update
Calling function
self.currentDate = Date()
let objTimeStamp = currentDate.getStringFromDateWithUTCFormat()
Perfect work when device time in 12-hour format when I try to change device time in 24-hour formate then given wrong time format.
set your dateFormatter locale to en-US then try to convert, it's works for me when iPhone's time format is 24 hour:
let date = Date()
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en-US")
formatter.dateFormat = "hh:mm a"
let time12 = formatter.string(from: date)
print(time12)
output:
01:37 PM

Manipulate date format in Swift 3.0

I have this datetime format output in UILabel
datefrom.text = "2010-12-05 00:00:00"
But I want to display this date format into something simple like this
5 December 2010 or Dec 5, 2010
I use this code to display the date but it display the today's date which is 5 April 2017
date.swift
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "d MMM yyyy"
let result = formatter.string(from:date)
datefrom.text = result
Any solution ?
Thanks.
The DateFormatter will help you do this!
Step 1: Convert your string to a date type
func convertStringToDate(string: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.date(from: dateString)
}
Step 2: Convert your date to your expected format
func convertDateToString(date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "d MMM yyyy"
return dateFormatter.string(from: date)
}
That's it!
let dateString = "2010-12-05 00:00:00"
let date = convertStringToDate(string: dateString)
let result = convertDateToString(date: date!)
datefrom.text = result
// 5 Dec 2010

Incorrect time in Date from 12-Hr format Time String - Swift

I have following strings as 12-Hr format time:
let timeFrom = "05:30 AM";
let timeTo = "04:35 PM";
And trying to get date from these for comparison as follows:
let openTime = timeFrom.date(format: "hh:mm a")
let closeTime = timeTo.date(format: "hh:mm a")
With this extension :
extension String
{
func date(format:String) -> Date?
{
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.timeZone = NSTimeZone.local
return formatter .date(from: self)
}
}
When I print openTime and closeTime, I'm getting incorrect values in time:
print(openTime) // 2000-01-01 00:00:00 +0000
print(closeTime) // 2000-01-01 11:00:00 +0000
Why this is so ? I think it is smoething related to time zone, so I visit NSDateFormatter return wrong date + Swift, but nothing worked for me. If anybody have the solution, please help me. Thank you !
I check with swift 3 and Xcode 8 with small changes its working please check
let timeFrom = "05:30 AM";
let timeTo = "04:35 PM";
let openTime = timeFrom.date(format: "hh:mm a")
let closeTime = timeTo.date(format: "hh:mm a")
print(openTime)
print(closeTime)
and make change in extension
extension String{
func date(format:String) -> Date?
{
let formatter = DateFormatter()
formatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
formatter.dateFormat = format
//formatter.timeZone = TimeZone.current //or autoupdatingCurrent
return formatter .date(from: self)
}}