When I run the following code
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
print(dateFormatter.string(from: Date(timeIntervalSince1970: 0)))
This gets printed out:
01/01/1970 08:00:00
which is supposed to be:
01/01/1970 00:00:00
what could be wrong?
Date(timeIntervalSince1970:) creates a date relative to 00:00:00 UTC on 1 January 1970 by the given number of seconds.
DateFormatter by default formats the date according to your local timezone.
Apparently you are in the GMT+8 timezone, so "00:00:00 UTC" is
"08:00:00" on your clock.
You can assign a different timezone to the date formatter if
required. To print the formatted date relative to UTC use
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) // <--- HERE
print(dateFormatter.string(from: Date(timeIntervalSince1970: 0)))
Try this and you will get the date according to your TimeZone :
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
let timeZoneDifference = TimeInterval(TimeZone.current.secondsFromGMT())
let date = dateFormatter.string(from: Date(timeIntervalSince1970: 0))
print(date)
let dateAccoringToMyTimeZone = dateFormatter.string(from:
Date(timeIntervalSince1970: timeZoneDifference))
print(dateAccoringToMyTimeZone)
Related
func getDateWithoutTimeForDisplay() -> String {
let formatter = DateFormatter()
// formatter.locale = Locale(identifier: "en_US")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = TimeZone(identifier: "UTC")
formatter.locale = Locale(identifier: "en_US_POSIX")
let myStringafd = formatter.date(from: self)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.timeZone = NSTimeZone.local
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
if let myStringafd = myStringafd {
let somedateString = dateFormatter.string(from: myStringafd)
return somedateString
}
return ""
}
this is my code to convert given UTC date to system date based on the local time zone.
The actual problem is, suppose the case "i have the time 1.00 AM on the date 25-08-2020, according to the Indian time zone, ( UTC time is 5.30 lesser than Indian time) the corresponding UTC date is 24-08-2020 due to the time difference with the Indian time". In this case i want to convert the UTC date(Because it is effecting the date to be display for the user) to the current system date.
My system date&time is 25-08-2020 1:00 AM (Indian time)
the corresponding UTC date is 24-08-2020. -5:30 hr
i need to convert the UTC date to the current local time
Try this out:
let currentDate = self.UTCToLocal(date: Date().description) //This will pass system date in UTC format to the function and function will return desired output in local timezone
func UTCToLocal(date: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssz" //The date format should be exact as same as UTC date
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = NSTimeZone.local
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss" //The desired date format in which you may want to return the value
return dateFormatter.string(from: dt!)
}
or you can check this post - Returning nil when converting string to date in swift and this link for dateFormatters - https://nsdateformatter.com/
I get the current UTC timestamp using Timezone(abbreviation:"UTC") but can't get the UTC timestamp of some other timestamp.
/*Current local time (4:15 PM) ---> Current UTC (11:15 AM)
Some other time (11:15 PM) ---> UTC (6:15 PM)*/
static func getDateFormat(by date:Date, format:String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.timeZone = TimeZone(abbreviation: "UTC")
formatter.locale = Locale.current
return formatter.string(from: date)
}
This always give the current UTC.
func convertToUTC(dateToConvert:String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy hh:mm a"
let convertedDate = formatter.date(from: dateToConvert)
formatter.timeZone = TimeZone(identifier: "UTC")
return formatter.string(from: convertedDate!)
}
Your question is not explained properly, though i got your question after reading it multiple times.
You want a function in which you provide a date and that function return you a new date in UTC format.
func convertDateToUTC(date: Date) -> Date {
let calendar = Calendar.current
let dateComponent = calendar.dateComponents([.timeZone], from: date)
//get difference of curent time zone from gmt
//subtract 0 as we want UTC from current time zone
let gmtDiff = 0-dateComponent.timeZone!.secondsFromGMT()
let dateStr = "11:15 PM"
let formatter = DateFormatter()
formatter.dateFormat = "h:mm a"
let date1 = formatter.date(from: dateStr)
let newDate = date1?.addingTimeInterval(TimeInterval(gmtDiff))
return newDate
}
You can format method as your convenience.
We need date components to get time zone difference of current to UTC, then we add that seconds to date and new date we get will be UTC date .
I'm using a standard DatePicker and printing the date on top of it.
For some reason my Date Picker has it's timezone set to IST, which maybe because of the locale.
Let's get into an example:
DatePicker -> 12:00 PM Date reads -> 06:30:00 +0000
Which is correct as the date will print UTC time and IST is UTC+5:30
Now when I change the timezone of the DatePicker, let's say to London which is GMT+1,
DatePicker -> 12:00 PM Date reads -> 06:30:00 +0000
Where in the date should actually read 11:00:00
How I am changing the DatePicker's timezone:
if let tmz = selectTimeZone {
datePicker.timeZone = tmz
}
i have the same issue sometime before but for the another reason. Try this hope it will help.
First -
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
var startDate = dateFormatter.string(from: yourdatepicker.date)
startDate = localToUTC(date: startDate)
Second we convert this to local to UTC , here we mention the TimeZone to system time
func localToUTC(date:String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = NSTimeZone.system
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dt = dateFormatter.date(from: date)
return dateFormatter.string(from: dt!)
}
Third if you want to conver UTC to your Local time
func UTCToLocal(date:String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = NSTimeZone.system
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter.string(from: dt!)
}
How to use Date Formatter in ios 10 swift 3?
i
You'll have to first convert the string to a date and then to a string in your required format. You can use DateFormatter to do this. Check the below code
let date = "07-11-2016 12:00:00"
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let convertedDate = formatter.date(from: date)
formatter.dateFormat = "d MMMM yyyy h a"
let dateString = formatter.string(from: convertedDate!)
print(dateString) // Output will be 7 November 2016 12 PM
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let date = dateFormatter.date(from: "07-11-2016 12:00:00")
dateFormatter.dateFormat = "dd MMMM yyyy HH a"
let dateString = dateFormatter.string(from: date)
dateString will be you desired output format of you date.
or you can simply create an extension of Date or string that return you the formatted date.
I know there are lot of questions passing around over this simple issue, but still I couldn't get a clear idea.
Here is what I want:
SelectedDateString = "19-08-2015 09:00 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm a"
dateFormatter.timeZone = NSTimeZone();
let SelectedUTCDate = dateFormatter.dateFromString(SelectedDateString)!
println("SelectedLocalDate = \(SelectedLocalDate)")
// OUTPUT: SelectedLocalDate = 2015-08-18 18:30:00 +0000
If I dont use TimeZone:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm a"
// dateFormatter.timeZone = NSTimeZone();
let SelectedUTCDate = dateFormatter.dateFromString(SelectedDateString)!
println("SelectedLocalDate = \(SelectedLocalDate)")
//OUTPUT: SelectedLocalDate = 2015-08-18 19:26:00 +0000
Why is there change in the time and Date? What I want is:
//OUTPUT: SelectedLocalDate = 2015-08-19 09:00:00 +0000
Also I want to convert the Local Date to Exact UTC Date
//Like this: SelectedUTCDate = 2015-08-19 03:30:00
inputString = "19-08-2015 10:45 am" // My localtime is 10:35 AM, so I set 10 mins from now
var currentUTCTime = NSDate() // currentUTCTime is 05: 15 AM.
I want to convert the inputString to its respective UTC Time and find the difference between the two times in both date and string.
//Like this Date: diffInDate: 00-00-0000 00:10 and
// Like this String: diffInString: 10 mins
How can I get both of these?
let dateString = "19-08-2015 09:00 AM"
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm a"
if let dateFromString = dateFormatter.date(from: dateString) {
print(dateFromString) // "2015-08-19 09:00:00 +0000"
dateFormatter.timeZone = .current
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm a Z"
dateFormatter.string(from: dateFromString) // 19-08-2015 06:00 AM -0300"
}
extension Date {
func originToString(dateFormat: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return dateFormatter.string(from: self)
}
}