How to use Date Formatter to get from 00:00:00 (through 23:00:00) to 1 - 12 am/pm format? - ios

I'm trying to get from 24 hrs like 5:00:00 to 5 am, but I'm having difficulty understanding how.
I have tried a few similar things, but eventually got to this:
let date: String = "5:00:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm:ss"
let theDate = dateFormatter.date(from: date)
dateFormatter.dateFormat = "HH:mm a"
myDateText.text = dateFormatter.string(from: theDate) // myDateText is a UILabel
I expected it to be 5 am but received an error that theDate is nil. I'm a newbie, so I would really appreciate any help.

HH is stand for 24 hours and hh is stand for 12 hours.
let date: String = "15:00:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
let theDate = dateFormatter.date(from: date)
dateFormatter.dateFormat = "hh:mm a"
print(dateFormatter.string(from: theDate!))
Output is 03:00 PM

Related

DateFormater error only when running on iOS 13

I have a function to convert string to date format. This function works as expected on iOS 12 but on iOS 13. I get this error:
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"
Here is my code:
func ConvertDateAndTimeFormat2() {
let timeDate = "2019-09-24 15:00:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
var dateObj:Date!
dateObj = dateFormatter.date(from: timeDate)
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
var timeObj:Date!
timeObj = timeFormatter.date(from: timeDate)
timeFormatter.dateFormat = "HH:mm"
timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
let timef = "\(timeFormatter.string(from: timeObj!))"
let Date = "\(dateFormatter.string(from: dateObj!))"
}
Please change your dateformat and use "yyyy-MM-dd HH:mm:ss Z" and try.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
For more details please visit https://nsdateformatter.com/
If you are trying to convert string time which in 24 hours format (Ex 15:25:00 / HH:mm:ss) then enable your device 24-Hour Time option. It might solve the issue in IOS 13.3
You may find the option here
Settings -> General -> Date & Time
There are many issues with your code. There is no reason to create two Date objects from one string. Just parse the original string once. Then you can create your date string and time string with the desired formats from that one date.
You should also use the special locale of en_US_POSIX when parsing fixed format date strings. There is also no need to set a timezone when parsing the original date string. The string provides its own timezone. The +0000 means it is UTC time.
You may not want to provide a timezone when converting the date to your final strings either. Most likely you want strings in the user's locale timezone, not some hardcoded timezone.
Here's your code cleaned up a lot:
func convertDateAndTimeFormat2() {
let timeDate = "2019-09-24 15:00:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
if let dateObj = dateFormatter.date(from: timeDate) {
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00") // Probably not needed
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:mm"
timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00") // Probably not needed
let timef = timeFormatter.string(from: dateObj)
let datef = dateFormatter.string(from: dateObj)
print("Date: \(datef), time: \(timef)")
}
}
convertDateAndTimeFormat2()
Output:
Date: Tuesday, Sep 24, 2019, time: 18:00

Swift: Local Date object and local timezone

I've searched and searched, but most of the time posts talk about creating a localized string from a Date(). I need a date object to set a Timer to fire at the correct time.
I realize I might be missing something... This is the best I have so far:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yy"
dateFormatter.locale = Locale.current
dateFormatter.timeZone = TimeZone.current
let todayDate = dateFormatter.date(from: "05/21/18")!
print(todayDate)
Which outputs:
2018-05-21 07:00:00 +0000
> Question: Why is there the +0000 if I set the dateFormatter variable timeZone to .current?
+ Update 01:
Following #MadProgrammer's comments:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yy HH:mm:ss z"
dateFormatter.locale = Locale.current
dateFormatter.timeZone = TimeZone.current
let dateString = dateFormatter.string(from: Date())
let todayDate = dateFormatter.date(from: dateString)!
print(dateString)
print(todayDate)
Which Outputs, first one being correct:
05/21/18 19:29:19 PDT
2018-05-22 02:29:19 +0000
> Question: Why would creating a date from the dateFormatter.string(...) date object be so different?
Thank you in advance!

How to format a date using timezone in Swift?

I have checked other questions but none of them helped me much.
I have following string:
let dateString = "2018-04-29T21:00:00.000Z"
I have successfully converted it to date using the following:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let convertedDate = dateFormatter.date(from: dateString)
But now I only want the time "hh:mm a" using timezone such as "+8". I have tried following way but it's not working:
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC+8")
dateFormatter.dateFormat = "hh:mm a"
let requiredTime = dateFormatter.string(from: convertedDate!)
Can anyone help me to overcome this problem?
The format you want is hh:mm a Z, which will provide the +0800.
You want to create a TimeZone which +8 hours from GMT, normally I prefer to use the appropriate abrivations (ie AET), but I guess if you don't have that, you can create a TimeZone using secondsFromGMT, for example...
let tz = TimeZone(secondsFromGMT: 8 * 60 * 60)
let toFormatter = DateFormatter()
toFormatter.timeZone = tz
toFormatter.dateFormat = "hh:mm a Z"
let requiredTime = toFormatter.string(from: convertedDate!)
Which based on your example data, will produce a value of...
05:00 AM +0800

swift NSDate AM and Pm Format [duplicate]

This question already has answers here:
NSDateFormatter- stringFromDate not returning AM/PM
(6 answers)
Closed 5 years ago.
To convert date and time, I have used this:
let formatter = NSDateFormatter()
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
formatter.dateFormat = "M/dd/yyyy, h:mm"
formatter.AMSymbol = "AM"
formatter.PMSymbol = "PM"
let dateString = formatter.stringFromDate(model.courseDate)
print(dateString)
If the course date is "courseDate = "2017-01-09 01:00:00 +0000";"
but I am getting like this in print:
1/09/2017, 1:00 with no any AM or PM.
How can I achieve this?
Thanks.
according to the date formatter specs
You can retrieve a date in 24h format instead of requesting AM/PM by using
HH instead of hh
so the date "21/11/2016 01:30" (pm) would be "21/11/2016 13:30".
if you do need to print the format, though, you can print it using:
a
More date fomatting details here
As per Apple general date formatter you should use "M/dd/yyyy, hh:mm a" in your date format.
like : formatter.dateFormat = "M/dd/yyyy, hh:mm a"
Hope this will help you!
use following code may help full for you
//Call function
let dateTime = self.convertDateFormaterAamer(sendTimeStep, inputDateFormate: "dd-MM-yyyy HH:mm:ss", outputDateFormate: "dd-MMM-yyyy hh:mm a")
//Date function
func convertDateFormater(date: String,inputDateFormate: String , outputDateFormate: String) -> String
{
let dateFormatter = NSDateFormatter()
//dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//dateFormatter.timeZone = NSTimeZone(name: "UTC")
dateFormatter.dateFormat = inputDateFormate
let date = dateFormatter.dateFromString(date)
//dateFormatter.dateFormat = "yyyy MMM EEEE HH:mm"
//dateFormatter.timeZone = NSTimeZone(name: "UTC")
dateFormatter.dateFormat = outputDateFormate
var timeStamp = ""
if (date != nil)
{
timeStamp = dateFormatter.stringFromDate(date!)
}
return timeStamp
}

swift - Converting String to NSDate [duplicate]

This question already has answers here:
NSDateFormatter return incorrect date from string
(3 answers)
Closed 7 years ago.
I'm trying to convert a Date and Time String to NSDate with Swift, But the problem is The time is not converting perfectly, Even i set my Timezone correctly, Here's my code:
var dateStr = "2015-10-16 08:00 AM"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 28800)
var date = dateFormatter.dateFromString(dateStr)
print(date!)
And it prints out like this: 2015-10-16 00:00:00 +0000 What do you think seems to be the problem? Thanks!
Nothing wrong here. You should convert date to String before print it.
let dateStr = "2015-10-16 08:00 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 28800)
let date = dateFormatter.dateFromString(dateStr)
print(dateFormatter.stringFromDate(date!))
or as in Swift 3
let dateStr = "2015-10-16 08:00 AM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 28800)
let date = dateFormatter.date(from: dateString)
print(dateFormatter.string(from: date))
The same result: 2015-10-16 08:00 AM

Resources