I'm new to programming.
How do I convert this String to local time in Swift?
var string = "7:55:26 PM"
Thanks!
this is how you format the string in to NSDate , deploy the following code
var dateFormat : NSDateFormatter = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd HH:mm:ss"
var string = "2015-03-27 07:55:26"
let date : NSDate = dateFormat.dateFromString(string)!
Related
I receive dates as String like this one:
"2016-05-20T12:25:00.0"
I want to get its corresponding NSDate object, and I'm trying this way:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-DDThh:mm:ss.s"
let date = dateFormatter.dateFromString(dateStr)
where dateStr is like the example I wrote first. I took the dateFormat string from this page, but I get a nil date, what is wrong there?
Thanks in advance
You have a few problems with your date format. First Y is for weekOfYear, D is for day of year. hh is used for 12 hours format, decimal second you should use capital S and you need to escape the 'T'
You should do as follow:
let dateString = "2016-05-20T12:25:00.0"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
if let date = dateFormatter.dateFromString(dateString) {
print(dateFormatter.stringFromDate(date) ) // "2016-05-20T12:25:00.0\n"
}
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
if let date = dateFormatter.stringFromDate(NSDate()) {
print(dateFormatter.stringFromDate(date) )
}
I've got a date stored in my database and I want to retrieve it and display it nicely in my tableview cells.
The format it comes in from the database and stored in option1 is BO05151530
Where the first two letters have meaning in the program but are not needed for the date so I take those off using the substringFromIndex function.
So what is left is 05151530 where it represents MMddhmm
I would like to display it nicely like MM-dd # h:mm a
For example 12-05 # 3:45 am
Here is what I tried but unfortunately ns_date1 comes up as nil each time.
What would you suggest I do?
let date1 = option1.substringFromIndex(option1.startIndex.advancedBy(2))
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMddhhmm"
let ns_date1 = dateFormatter.dateFromString(date1)
Try this. you don't need to separate BO NSDateFormatter can handle extra string
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "'BO'MMddHHmm"
let ns_date1 = dateFormatter.dateFromString("BO05151530")
dateFormatter.dateFormat = "'BO' MM-dd # hh':'mm a"
let string = dateFormatter.stringFromDate(ns_date1!)
try
dateFormatter.dateFormat = "MMddHHmm"
HH is 24 hour format and hh is 12 hour format. you need the 24 one
Check out this app, this will help you know the right format and give you code
https://itunes.apple.com/ae/app/date-format-creator/id965645209?mt=12
NOTE: you need to add year to get a correct NSDate
I have this function :
class func getTheDateString(stringForInputDate: String, fromFormat inputFormat: String, toFormat outputFormat: String) -> String {
let formatter: NSDateFormatter = NSDateFormatter()
formatter.dateFormat = inputFormat
formatter.timeZone = NSTimeZone.localTimeZone()
let dateForInput: NSDate = formatter.dateFromString(stringForInputDate)!
formatter.dateFormat = outputFormat
return formatter.stringFromDate(dateForInput)
}
and used it as:
let stringDate : String = "0515930" // "05151530"
if stringDate.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) < 8 {
print([ViewControllerForScreen1 .getTheDateString(stringDate, fromFormat: "MMddHmm", toFormat: "MM-dd # h:mm a")]);
}else {
print([ViewControllerForScreen1 .getTheDateString(stringDate, fromFormat: "MMddHHmm", toFormat: "MM-dd # h:mm a")]);
}
OUTPUT:
["05-15 # 9:30 AM"]
["05-15 # 3:30 PM"]
I have this date 2015-11-06T18:00:00-0500
My format is yyyy-MM-dd'T'HH:mm:ssZ
i try
let startDateString = "2015-11-06T18:00:00-0500"
let format = NSDateFormatter()
format.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let startDateBtnEnd = format.dateFromString(startDateString)
println("startDateBtnEnd 2 \(startDateBtnEnd)")
But the log is
startDateBtnEnd 2 Optional(2015-11-07 00:00:00 +0000)
NSDate stores dates in UTC. You can convert it to the same moment in time in any timezone. But after losing too many neurons to mentally convert NSDate from one timezone to another, I decided to add my own extension to NSDate to print it out in the local timezone instead:
extension NSDate {
func toString(timeZone: NSTimeZone = NSTimeZone.localTimeZone()) -> String {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
formatter.timeZone = timeZone
return formatter.stringFromDate(self)
}
}
// Usage:
print("startDateBtnEnd 2 \(startDateBtnEnd.toString())")
My string is = "2015-08-26 14:21:40.557"
My code:
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:Z"
var date = dateFormatter.dateFromString(creationDateStr as String )
I get a nil date , what's the right format?
The format must match your input string exactly, so
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
will do the job.
Note that Z is used for the timezone, not for (milli)seconds.
And HH is for 24 hours; hh only allows values between 01 and 12 and is usually paired with a to indicate AM/PM.
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
var date = dateFormatter.dateFromString(creationDateStr as String )
Will return nil if your string is not in this date format. (ex. "5/26/2015" will return nil but "2015-05-26 12:30:00.123" will return a nsdate)
The format is incorrect for the string you are attempting to parse.
The correct format is:
"yyyy-MM-dd HH:mm:ss.SSS"
Parsing an incorrect format will return a nil value.
var str = "2015-08-26 14:21:40.557"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
var date = dateFormatter.dateFromString(str)
Quick question.
I have a string date: example var dateString = "17-02-2015"
I use:
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
var newDate = dateFormatter.dateFromString(dateString)
And I get the output: 2015-01-16 23:02:00 +0000
Why am I missing a day? Is it because of the time zone? (I am at +1, and in the date the timezone is 0?).
I also tryed setting the time zone:
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.timeZone = NSTimeZone.systemTimeZone()
But no success.
What am I missing?
Thats the UTC time and it is correct considering the fact that your LocalTime is 1+
var dateString = "17-02-2015"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
var newDate = dateFormatter.dateFromString(dateString)!
println(newDate.descriptionWithLocale(NSLocale.currentLocale())!)