GOAL: I want my string to be date formatted and put inside a label.
Current code:
let dateString = "2015-10-09 17:41:14"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM d, yyyy | hh:mm a"
let date = dateFormatter.dateFromString(dateString)
//FOUND NIL HERE
let NewString = dateFormatter.stringFromDate(date!)
Date.text = NewString
The code above works if I do NSDate() but not if I use my own string?
You are using wrong date format to convert the string to date and it returns a nil date because of that. First you need to use the exact date format to convert the string to date then set your date formatter style to get it back as string in your desired format.
Change your code like:
let dateString = "2015-10-09 17:41:14"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString(dateString)
dateFormatter.dateFormat = "MMMM d, yyyy | hh:mm a"
let resultString = dateFormatter.stringFromDate(date!)
print(resultString) // October 9, 2015 | 05:41 PM
Related
I am getting date string from server like that "2017-08-05T00:30:00.000+02:00". Below is my code for date formatter.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
But it returns 2017-08-04 22:30:00 +0000. Which is wrong. Any suggestions?
In Swift 3
You can change your server time string to UTC time Date as:
let dateFormatter: DateFormatter = DateFormatter()
var serverDateString = "2017-08-05T00:30:00.000+02:00"
let index = serverDateString.index(serverDateString.startIndex, offsetBy: 19)
serverDateString = serverDateString.substring(to: index)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.init(identifier: "UTC")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Your Code for parsing date is correct: 2017-08-05T00:30:00.000+02:00 and 2017-08-04 22:30:00 +0000 represent the same time, just in different time zones. Your only problem is that Date doesn't actually store the time zone
yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ
This is correct date formate for your input string. Here is the Apple Document for more description.
Here is my code:
var strInputDateString: String = "2017-08-05T00:30:00.000+02:00"
var dateFormat = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ"
//Set new dateFormate
var date1: Date? = dateFormat.date(from: strInputDateString)
dateFormat.dateFormat = "dd-MM-YYYY hh:mm:ss"
// Your Desire
var strOutputDateString: String = dateFormat.string(from: date1!)
print("\(strInputDateString)")
print("\(strOutputDateString)")
output:
Main input String:2017-08-05T00:30:00.000+02:00
Converted String: 05-08-2017 04:00:00
For now, I tried to convert your date format with my custom. It's give me the perfect output.
Actually, your code is good.
I copied your data and ran it in playground.
What kind of format you want it be?
Try this
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Hope this helps
The step you are missing is to again use your DateFormatter to format the date into a string. When you use print on the date, it will always show GMT. For illustration purposes, I've set the TimeZone to Berlin to match the original offset of +2:00.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
// Hard set to Berlin
dateFormatter.timeZone = TimeZone(identifier: "Europe/Berlin")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
// Your missing step
dateFormatter.string(from: currentDate)
Remember that DateFormatters are about:
Parsing from a String - df.date(from: String)
Formatting from a Date - df.string(from: Date)
HI I am trying to formate date from string (08-05-1988) I want to convert to date as given below, output also given below,
let dateString = "08-05-1988"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateFromString = dateFormatter.date(from: dateString)
let stringValue = String(describing: dateFromString)
out put : Optional(1988-05-07 18:30:00 +0000)
Then, I want to format the out put date from (1988-05-07 18:30:00 +0000) to other format as given below. But, when format the string to Date is nil
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd hh:mm:ss Z"
let date = dateFormatter.date(from: dateString)
let formatter = DateFormatter()
formatter.dateFormat = "dd-MMM-yyyy"
let formatedDate:String = formatter.string(from: date!)
Expected Output : 08-May-1988
The output Optional(1988-05-07 18:30:00 +0000) is the result of the description of a Date object. It's not related to any given string format.
To convert 08-05-1988 to 08-May-1988 you can simply use
let dateString = "08-05-1988"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateFromString = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "dd-MMM-yyyy"
let formatedDate = dateFormatter.string(from: dateFromString!)
let date = Date()
let format = DateFormatter()
format.dateFormat = "dd-MM-yyyy"
let formatDate = format.string(from: date)
You're calling dateFormatter.date on dateString instead of dateFromString. Since "08-05-1988" doesn't match the specified format of "yyyy-mm-dd hh:mm:ss Z", dateFormatter.date returns nil.
I have a string that is a date in this format 03/02/2017
I would like to display it like this Mar 2, 2017
I've been trying to get this formatter to work, but it keeps giving me nil, and I don't think I'm setting it in the correct format.
// convert saved date from string
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "mm/dd/yyyy" //Your date format
let date = dateFormatter.date(from: '03/02/2017')
print("\(date) save date") // nil
historyEntry.date = date as NSDate?
is not in
dateFormatter.dateFormat = "mm/dd/yyyy"
it is in
dateFormatter.dateFormat = "MM/dd/yyyy"
for full output
let dateFormatter = DateFormatter()
// iinitiallly set the date format based on your date
dateFormatter.dateFormat = "MM/dd/yyyy" //Your date format
// convert the string to date , but date is not the proper format , so dont worry about this
let date = dateFormatter.date(from: "03/02/2017")
// again reassign what the output you need set the date format
dateFormatter.dateFormat = "MMM dd, yyyy" // if you need the same as Mar 2, 2017 then use "MMM d, yyyy"
// finally convert your date to string
let myStringafd = dateFormatter.string(from: date!)
print("\(myStringafd) save date")
output
let dateString = "03/02/2017"
let df = DateFormatter()
df.dateFormat = "MM/dd/yyyy" // this dateformat should be your current string's date format
df.timeZone = TimeZone(identifier: "UTC-05:00") // if there is timezone you want to set
if let date = df.date(from: dateString) {
df.dateFormat = "MMM dd, yyyy" // This dateformat is that you want as result
let stringFromDate = df.string(from: date)
cell.lblDate.text = stringFromDate
}
From response I have set time in String, its contains HH: mm format the task is I have to convert that string into 12hrs time interval with AM and PM in another string to display results
For example "16:36:00.000Z" I want my result like 4:36 PM
Here my sample code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString(dateValue)
// To convert the date into an HH:mm format
dateFormatter.dateFormat = "HH:mm"
let dateString = dateFormatter.stringFromDate(date!)
print(dateString)
But unfortunately I'm getting nil in date
do like
let dateFormatter = NSDateFormatter()
16:36:00.000Z
dateFormatter.dateFormat = "HH:mm:ss.sssZ"
let date = dateFormatter.dateFromString(dateValue)
// To convert the date into an HH:mm format
dateFormatter.dateFormat = "hh:mm a" // or //h:mm a
let dateString = dateFormatter.stringFromDate(date!)
print(dateString)
How can i achieve this kind of format?? using "NSDateFormatter"
March 1-2 2016?
Is there a format to get day only, Month only, Year only?
let dateString = self.dateLists[indexPath.row]
let dateFormatter2 = NSDateFormatter()
dateFormatter2.dateFormat = "MMMM"
let date2 = dateFormatter2.dateFromString(dateString as String)
You need the correct dateformat to get everything you want from an Datestring.
If your date is a string, you need to convert it first to an NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd" // the format of your string
dateFormatter.timeZone = NSTimeZone(name: "GMT")
if let date = dateFormatter.dateFromString(dateString) {
// conversation to NSDate() was successful
// now convert to anything you want
dateFormatter.dateFormat = "MMMM"
let newDateString = dateFormatter.stringFromDate(date)
}
Here are all available Dateformatters, for example:
Month only (as text):
“MMMM”
Or as 2 digit String:
“MM”
Here is all you need:
http://www.codingexplorer.com/swiftly-getting-human-readable-date-nsdateformatter/
let str1 = "May 12 1968 04:00 PM"
let str2 = "May"
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "MM"
let date1 = dateFormatter.dateFromString(str1) // nil
let date2 = dateFormatter.dateFromString(str2) // May 1, 2000, 12:00 AM
dateFormatter.dateFormat = "MM dd yyyy hh:mm a"
let date3 = dateFormatter.dateFromString(str1) // May 12, 1968, 04:00 PM
let date4 = dateFormatter.dateFromString(str2) // nil
check your input string ... it must conform to you format
check your locale, even though you define your custom format, the
system use the locale (gregorian vs julian calendar ... etc)
if it still doesn't work, parse you string 'manually'