date is not showing after convert the string to date in swift - ios

I have strings and converting into the date format, but when I convert after the date is showing properly but time is showing the wrong format
firsDateString : 2019-Mar-15 9:00 AM
secondName : 2019-Mar-15 8:00 PM
String convert into the date format
let addedDate = firsDateString
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MMM-dd hh:mm a"
let date1 = formatter.date(from: addedDate)
print("DATE \(date1)")
After that am getting this values:
DATE Optional(2019-03-15 03:30:00 +0000)
How can get the correct date format?

use like , but is not a valid answer may be it works based on your condition but follow option2 it will cover all scenario if you need
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MMM-dd hh:mm a"
let date1 = formatter.date(from: "2019-Mar-15 9:00 AM")
formatter.amSymbol = ""
formatter.pmSymbol = ""
let currentDateStr = formatter.string(from: date1!)
print("currentDateStr (currentDateStr)")
based on #JoakimDanielson point
How can this be the accepted answer when you no longer can tell the difference between 8 in the morning and 8 in the evening
here you need to follow the 24 hour format , I update my answer ,
Option 2
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MMM-dd hh:mm a"
let date1 = formatter.date(from: "2019-Mar-15 9:00 AM")
formatter.dateFormat = "yyyy-MMM-dd HH:mm"
let currentDateStr = formatter.string(from: date1!)
print("currentDateStr \(currentDateStr)")
Output

Check this:
let addedDate = firsDateString
let formatter = DateFormatter()
formatter.timeZone = (NSTimeZone(name: "UTC")! as TimeZone)
formatter.dateFormat = "yyyy-MMM-dd hh:mm a"
let date1 = formatter.date(from: addedDate)
print("DATE \(String(describing: date1!))")

Related

Convert string to particular date format getting nil in iOS

I am getting date in string format from server. So I am trying to formatting to particular format.
But it is getting nil.
"07September16 4:09 am", This I want to format like Sep 07 2016
I am doing it with following code
dateLabel.text = model().formatStringToDate(dateString: date)
func formatStringToDate(dateString: String) -> String {
let dateStr = dateString
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d yyyy"
let formattedDate = dateFormatter.date(from: dateStr)
print("formattedDate \(formattedDate)")
//I have to return formatted string here
}
Any suggestions?
That should work;
// create `Date` from date string
let dateStr = "07September16 4:09 am"
let dateFormatterGet = DateFormatter()
dateFormatterGet.locale = Locale(identifier: "en_US_POSIX")
dateFormatterGet.dateFormat = "ddMMMMyy h:mm a"
let date = dateFormatterGet.date(from: dateStr)
// convert back Date to string as Sep 07 2016
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd yyyy"
print(dateFormatterPrint.string(from: date!))
let dateString = "07September16 4:09 am"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "ddMMMyy h:mm a"
let dateObj = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "MMM dd yyyy"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")
First you must get date from your string with current format like`
let dateStr = dateString
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "ddMMMMyy h:mm a"
let formattedDate = dateFormatter.date(from: dateStr)!
print("formattedDate \(formattedDate)")
After getting the date you can get date string in which format you want just change the format`
dateFormatter.dateFormat = "MMM dd YYYY"
let convertedDateString = dateFormatter.string(from: formattedDate)

Format Date from String - swift3

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.

Use Date Formatter iOS10 Swift3

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.

How to convert string 24hrs type time into 12hrs time

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)

NSDateFormatter in IOS SWIFT 2

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'

Resources