UIDatePicker getting GTM value - iphone

I need to sent a date with a timezone value in GTM. How can you get this from UIDatePicker?
I need the following format:
"02/Jan/2006:15:04:05 -0700
I have tried this in swift:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy:HH:mm"
dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
let strDate = dateFormatter.stringFromDate(myDatePicker.date)
self.selectedDate.text = strDate
and also this:
self.myDatePicker.timeZone = NSTimeZone.localTimeZone()

the letter for the timezone is zfor the name of the timezone and Zfor the difference to GMT if this is what you are asking for
dateFormatter.dateFormat = "dd/MM/yyyy:HH:mm:ss Z"
http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns

Related

how to change date format 2021-30-06T05:00:00+07:00 in swift?

i want to change date format from 2021-30-06T05:00:00+07:00 to get only time with format hh:mm:ss, but i didn't get it.
here is my code i already try
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "GMT+7")
let date = dateFormatter.dateFromString("2021-30-06T05:00:00+07:00")
// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "HH:mm:ss"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
let timeStamp = dateFormatter.stringFromDate(date!)
print(timeStamp)
Your 2021-30-06T05:00:00+07:00 is not matching with yyyy-MM-dd'T'HH:mm:ss.
Date format should be yyyy-dd-MM'T'HH:mm:ssZZZZZ.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-dd-MM'T'HH:mm:ssZZZZZ"
let date = dateFormatter.date(from: "2021-30-06T05:00:00+07:00")
// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
let timeStamp = dateFormatter.string(from: date!)
print(timeStamp)
Output:
03:30:00 // This is as per my current timezone.
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
dateFormatter.locale = Locale.current
print(dateFormatter.string(from: post.createdDate))
Use DateFormatter instead of NSDateFormatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-dd-MM'T'HH:mm:ssZZZZZ"
let yourDate = dateFormatter.date(from: "2021-30-06T05:00:00+07:00")
// change to local time zone from your format
dateFormatter.dateFormat = "dd MMMM yyyy HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
let DateString = dateFormatter.string(from: yourDate!)
print(DateString)

Convert UTC DateTime to local datetime does not show correctly in swift

I am using swift 4. I need to convert my UTC Time to local time.But it shows two hour before. I am now Living in dhaka with GMT+6. Here is code i used ..
var date:String = "2020-02-10 14:30:57 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd H:mm:ss a"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "yyyy-MM-dd h:mm a"
let convertedLocalTime = dateFormatter.string(from: dt!)
tv_time.text = convertedLocalTime
print("convertedLocalTime-->",convertedLocalTime)
It shows 2020-02-10 6:30 PM But now 8.30 PM . What is the wrong with the code
Please help me
Your input data is not correctly formatted.
If you can use a correct format for the input data:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd H:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
// set the date in UTC
let date = dateFormatter.date(from: "2020-02-10 14:30:57")
dateFormatter.timeZone = TimeZone.current
// String representation of the date for local time
let s = dateFormatter.string(from: date!)
this snippet sets s to "2020-02-10 15:30:57" which is correct for Germany.
Otherwise you have to correct the format of your input data, e.g. delete the "PM" from the 24h format string.

How can I determine the time zone of a given (local) date

From an API I get the following two date strings. Those dates show the time of a weather observation of a weather station that’s located in some time zone.
dateUTC = "2019-06-14T18:00:00Z"
dateLocal = "2019-06-14 20:00:00"
How can I determine the time zone the weather station is in?
(Not the time zone of the user!)
I've tried to use the DateFormatter, but unfortunately the local date gets immediately transformed to GMT - and it's then identical with the UTC date.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let dateUTC = dateFormatter.date(from: current.currentObservation[0].timeUTC)!
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateLocal = dateFormatter.date(from: current.currentObservation[0].timeLocal)!
I would like to see as the result of the above date(s):
Time zone = GMT+2
The answer was quite simple - still I didn't see it for hours. The trick is to set a timeZone for the DateFormatter.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let dateUTC = dateFormatter.date(from: current.currentObservation[0].timeUTC)!
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dateLocal = dateFormatter.date(from: current.currentObservation[0].timeLocal)!
let difference = Calendar.current.dateComponents([.second], from: dateUTC, to: dateLocal).second!

How can i get Local DateFormat with swift?

I have CEST dates in yyyy-MM-ddTHH:mm:ssZ format. I want to show them in local format. The following function works fine. It shows the time correctly.
But in the end I am forced to show the date in "dd.MM.yyyy HH:mm" format. How can i get Local/native DateFormat from iOS.
func changeTime()
{
myLabel.text = convertTimeZoneToLocal(timeZone: "CEST", date: "2017-09-27T18:25:42Z")
}
func convertTimeZoneToLocal(timeZone:String, date:String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = TimeZone(abbreviation: timeZone)
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "dd.MM.yyyy HH:mm" // I want to change this line.
return dateFormatter.string(from: dt!)
}
You can use :
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
But these won't come in the same format dd.MM.yyyy HH:mm
// create dateFormatter with UTC time format
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString("2015-04-01T11:42:00")// create date from string
// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
let timeStamp = dateFormatter.stringFromDate(date!)
Use this to get the the formate base on selected region of OS
let dateformatter = DateFormatter()
dateformatter.setLocalizedDateFormatFromTemplate("dd/MM/yyyy")
let strDate = dateformatter.string(from: NSDate() as Date)
Output :
OS Region is US : 18/02/2021
OS Region is US : 02/18/2021

UTC to local time conversion not working in Swift 2

I am trying to convert a UTC time to local timezone, but it doesn't seem to be converting it at all:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date1 = dateFormatter.dateFromString(dateStringFromServer)!
dateFormatter.timeZone = NSTimeZone.localTimeZone()
let date2 = dateFormatter.dateFromString(dateStringFromServer)!
The dateStringFromServer is a string representation of a UTC date. So I was expecting date1 to be in UTC, and date2 to be in PDT (my local time zone), but they are both the same. Something wrong with my syntax?
This is what I'm getting:
dateStringFromServer: 2016-10-21T05:24:26.000Z
date1: 2016-10-21 05:24:26 +0000
date2: 2016-10-21 05:24:26 +0000
How can I get date2 be in the device's local timezone?
If you want to convert to the time zone set on the device you can do this
Swift3
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date1 = dateFormatter.date(from: dateStringFromServer)
// return the timeZone of your device i.e. America/Los_angeles
let timeZone = TimeZone.autoupdatingCurrent.identifier as String
dateFormatter.timeZone = TimeZone(identifier: timeZone)
let date2 = dateFormatter.string(from: date1)
Swift2
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date1 = dateFormatter.dateFromString(dateStringFromServer)
// return the timeZone of your device i.e. America/Los_angeles
let timeZone = NSTimeZone.localTimeZone().name
dateFormatter.timeZone = NSTimeZone(name: timeZone)
let date2 = dateFormatter.stringFromDate(date1!)
It's the same because even though you think you're setting the date formatter's time zone to the current time zone after setting the first date, it doesn't matter. If you don't set the date formatter's time zone, it automatically sets to the system time zone as specified in Apple's docs for NSDateFormatter:
If unspecified, the system time zone is used.
Therefore the date formatter's time zone is set to be the same both implicitly for the first date and explicitly for the second date because they happen to be identical, hence you're getting the same date back.
Here is the code that work in swift4.
func UTCToLocal(date:String, fromFormat: String, toFormat: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = fromFormat
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = toFormat
return dateFormatter.string(from: dt!)
}
How to use :-
let localDateAsString = UTCToLocal(date: deadline!, fromFormat: "yyyy-MM-dd HH:mm:ss", toFormat: "MM-dd-yyyy hh:mm a")