Convert date string into proper format - ios

I am getting a response from a server and there is a date string I need to convert into a date:
Thu Jun 29 07:15:25 +0000 2017
I am trying to convert the string into a human readable format. Can anyone suggest how to convert this string into date?

You need to parse the date string into a Date object using a DateFormatter, then create a string from that Date using another DateFormatter with the output format you want to use.
let created_at = "Thu Jun 29 07:15:25 +0000 2017"
let df = DateFormatter()
df.dateFormat = "E MMM dd HH:mm:ss Z yyyy"
guard let date = df.date(from: created_at) else {return}
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = "EEE, MMM d"
let outputDate = outputFormatter.string(from: date)
If you want to know what date formats look like, use NSDateFormatter.com, which has an interactive interface where you can play around with different date formats and have examples of most common formats as well.

This is how you can format it in "Thu Jun 29"
-(NSString *)formatCreatedAt:(NSString *)createdAt
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *date = [dateFormat dateFromString:createdAt];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd"];
NSString *stringFromDate = [formatter stringFromDate:date];
return stringFromDate;
}
Pass the value of created_at in this method and the output will be returned in your specified format.

Related

Convert String to Date in Objective-C [duplicate]

This question already has an answer here:
Converting NSString to NSDate - wrong format
(1 answer)
Closed 4 years ago.
How do i convert string to date in Objective C.
I have tried the following but did not figure it out.
NSString *str = #"3/2/2018 11:44:32 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/d/yyyy h:mm:ss a"];
NSDate *resultDate = [[NSDate alloc] init];
resultDate = [df dateFromString:str];
NSLog(#"result date: %#", resultDate);
result date: 2018-03-02 06:14:32 +0000 ,but i need to get it as
result date: 3/2/2018 11:44:32 AM
You are correctly parsing the string to the Date object. The way it is presented by the print is because by default if printing an object, its description is printed. In case of Date, it will be always the format you get. But the date is correct.
If you want to get it presented the way it was before, again use the same dateFormatter and just format the date to string back:
NSLog(#"result date: %#", [df stringFromDate:resultDate]);
UPDATE
If the problem is the hour shift, that's due to your current timezone that will be used when parsing using DateFormatter. To overcome this, set explicitly timezone and locale of the date formatter, see this example (swift version, but you need just those two line with setting timeZone and locale on dateFormatter):
let dateString = "3/2/2018 11:44:32 AM"
let df = DateFormatter()
df.dateFormat = "MM/d/yyyy h:mm:ss a"
// set the timezone and locale of the dateformatter:
df.timeZone = TimeZone(identifier: "GMT")
df.locale = Locale(identifier: "en_US_POSIX")
let date = df.date(from: dateString)
// now it will print as you expect:
print(date)
Your issue is related to your app current locale, if your current locale is "en_US" then your NSLog(#"result date: %#", resultDate); line will print
result date: Fri Mar 2 11:44:32 2018
Maybe, this code will help you, again that date its convert to Nsstring, check it this code
-(void)DateChange
{
NSString *Input_Date =#"3/22/2018 11:44:32 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"M/d/yyyy hh:mm:ss a"];
NSDate * Format_date = [dateFormatter dateFromString:Input_Date];
[dateFormatter setDateFormat:#"M/d/yyyy hh:mm:ss a"];
NSString *Change_date = [dateFormatter stringFromDate:Format_date];
NSLog(#"Final Change Date :- %#",Change_date);
}
My Output is :-
Final Change Date :- 3/22/2018 11:44:32 AM

how do i convert a string to specific format

I have referred lot of links but could not find the solution hence asking it again.I have string which i need to convert into date format. Below is example
Input string : "1410271500"
In above string 14 is for 2014
10 is for Oct
27 is day
1500 is 3:00 PM
Output string : "2014-10-27 03:00:00"
Input string in not in long-integer format.
Thanks
do like
NSString *getMilliSec = #"1410271500";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyMMddHHmm"];
NSDate *getDate = [formatter dateFromString:getMilliSec];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"]; // or use yyyy-MM-dd hh:mm:ss
formatter.timeZone = [NSTimeZone systemTimeZone]; // optional
NSString *stringFromDate = [formatter stringFromDate:getDate];
swift3
let getMilliSec = "1410271500"
let formatter = DateFormatter()
formatter.dateFormat = "yyMMddHHmm"
let getDate = formatter.date(from: getMilliSec)
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss a" // or use yyyy-MM-dd hh:mm:ss
formatter.timeZone = NSTimeZone.system // optional
var stringFromDate = formatter.string(from: getDate!)
output

How to convert a date string in one format into another format? [duplicate]

I need to convert a date string retrieved from a server, to local NSDate, the string looks like:
"Fri, 30 Nov 2012 00:35:18 GMT"
But now it is Friday, 30th Nov, 11:36 AM, so how do I convert the string to a meaningful local NSDate?
I found:
iPhone: NSDate convert GMT to local time
But looks like it does the opposite.
Note that NSDate's always store the date, internally, in GMT. You then use a date formatter to create a string in your local time zone. In this case, you are starting with a string so need to use the date formatter to create the date in the first place, and then use it again to create a string in your local time zone (which it defaults to when the timezone is not specified).
NSString *dateString = #"Fri, 30 Nov 2012 00:35:18 GMT";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = #"EEE, dd MM yyyy HH:mm:ss zzz";
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *localDateString = [dateFormatter stringFromDate:date];
NSLog(#"%#", localDateString);
// Results: Thu, 29 11 2012 19:35:18 EST
Just use NSDateFormatter with the formatting string eee, dd MMM yyyy HH:mm:ss zzz. The Zs at the end cause it to read the time zone from the string.
So, given QA1480, sample code:
NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setLocale:usLocale];
[formatter setDateFormat:#"eee, dd MMM yyyy HH:mm:ss zzz"];
_createdAt = [formatter dateFromString:dateString];

NSDateFormatter not Working for Twitter date

I am trying to format a date i am getting from twitter using the STTwitter library.
However the code that I've tried so far has not worked.
Code for getting the date from twitter:
NSString *dateString = [status valueForKey:#"created_at"];
This returns the time, date, time zone and year in which the tweet was made which looks messy.
I tried using the following code to convert this and make it neater:
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMddHHmm"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#", dateFromString);
dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd MMMM' at 'hhmm a"];
NSString *mydate=[dateFormatter stringFromDate:dateFromString];
And then try to put the result in a text label:
cell.detailTextLabel.text = my date;
Ive tried many different variations of the Date Formatter but none have worked and i have no idea why.
Thanks for your help :)
The date format you are using is not even close the date string used in the result, which is something like Fri Nov 18 20:35:49 +0000 2011.
NSString *dateStr = #"Fri Nov 18 20:35:49 +0000 2011";
NSDateFormatter *dateFormatter= [NSDateFormatter new];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *date = [dateFormatter dateFromString:dateStr];
The real trick is in the locale used, since the date is localized in english.
STTwitter has a category for that:
NSString *s = [tweet valueForKey:#"created_at"];
NSDate *date = [[NSDateFormatter stTwitterDateFormatter] dateFromString:s];
I created a gist with Swift implementation of it:
https://gist.github.com/appzzman/62339fcd10bbe8fce256 It takes Twitter date and lets you specify the output format of the date.
import UIKit
func parseTwitterDate(twitterDate:String, outputDateFormat:String)->String?{
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
var indate = formatter.dateFromString(twitterDate)
var outputFormatter = NSDateFormatter()
outputFormatter.dateFormat = "hh:mm a dd:MM:yy"
var outputDate:String?
if let d = indate {
outputDate = outputFormatter.stringFromDate(d)
}
return outputDate;
}
var str = "Wed Sep 02 19:38:03 +0000 2009"
var outputDateFormat = "hh:mm a dd:MM:yy"
parseTwitterDate(str, outputDateFormat)

NSDate to RFC 2822 Date format

Is there any efficient way to convert an NSDate to RFC 2822 Date format string ?
I want to use this string to create an NSURLRequest and set the value of "If-Modified-Since" header field.
try to use an NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // maybe there exist a new-method now
dateFormatter.dateFormat = #"EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format
NSString *dateString = [dateFormatter stringFromDate:myDate];
Swift 3,4,5
let rfcDateFormat = DateFormatter()
rfcDateFormat.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
let dateString = rfcDateFormat.string(from: Date())
//today result: Mon, 06 Feb 2017 17:21:18 +0100

Resources