NSDateFormatter not Working for Twitter date - ios

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)

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

coversion of date and time from 31 May 2016 04:30 PM(current format) to 2016-05-31T16:30:00.000+05:30(Required format)

hi can any on help me in achiving this is the string which i have 31 May 2016 04:30 PM(NSSTring) 2016-05-31T16:30:00.000+05:30(Required Format)
NSString *dateString = dateandtime;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
used the following code but returns nil
The current format string does not contain any time zone information, so you have to set the time zone in the date formatter as well as the locale to be able to parse the string independently from the current locale.
The input format is dd MMM yyyy hh:mm a
NSString *dateString = #"31 May 2016 04:30 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
Some how I tried and worked
NSString *myString = dateandtime;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
Whatever you do, test that your code works if the user doesn't use an Indian timezone, and if the user uses 24 hour time instead of AM/PM on their phone. I assume your "dateandtime" was created by converting an NSDate to an NSString; it would be much better to not do that but start with the original NSDate.
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = #"2016-05-31T16:30:00.000+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
you can use this code.

NSDateFormatter with custom string inbetween

I'm new to iOS app development. It will be great if someone helps me. I have a NSDateFormatter with the date format like M/d/yy hh:mm a and it displays correct result as
3/24/2016 12:00AM.
But i want to add String
at in between date and time. Expecting result as
3/24/2016 at 12:00AM
Your format could be something like
M/dd/yyyy' at 'hh:mm a
3/24/2016 at 12:00 AM
You can check this link for further details.
Format String Output String
M/d/y 11/4/2012
MM/dd/yy 11/04/12
MMM d, ''yy Nov 4, '12
MMMM November
E Sun
EEEE Sunday
'Week' w 'of 52' Week 45 of 52
'Day' D 'of 365' Day 309 of 365
QQQ Q4
QQQQ 4th quarter
m 'minutes past' h 9 minutes past 8
h:mm a 8:09 PM
HH:mm:ss's' 20:09:00s
HH:mm:ss:SS 20:09:00:00
h:mm a zz 8:09 PM CST
h:mm a zzzz 8:09 PM Central Standard Time
yyyy-MM-dd HH:mm:ss Z 2012-11-04 20:09:00 -0600
You can combine any of the options as per your requirements.
To do it properly, with localization in mind, you will have to localize at, too:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterShortStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
timeFormatter.dateStyle = NSDateFormatterNoStyle;
timeFormatter.timeStyle = NSDateFormatterShortStyle;
// load date format from localization files
NSString *dateFormat = #"%1$# at %2$#"; //NSLocalizedString(#"my_date_format", #"My date format");
NSDate *date = [NSDate date];
NSString *localizedDate = [NSString stringWithFormat:dateFormat, [dateFormatter stringFromDate:date], [timeFormatter stringFromDate:date]];
NSLog(#"%#", localizedDate);
Also note that I didn't hardcode date formats but I have used the ones defined by the user (language & region settings) instead.
You just need to M/d/yy' at 'hh:mm a as formatter.
Please use below code for swift Language.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "M/dd/yyyy"
var result: NSString = dateFormatter.stringFromDate(NSDate())
let timeFormatter = NSDateFormatter()
timeFormatter.dateFormat = "hh:mm a"
result = NSString(format: "%# at %#", result,timeFormatter.stringFromDate(NSDate()))
print(result)
Also, if you are doing development in Objective C then you can use below code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"M/dd/yyyy";
NSString *result = [NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:[NSDate date]]];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
timeFormatter.dateFormat = #"hh:mm a";
result = [NSString stringWithFormat:#"%# at %#",result,[timeFormatter stringFromDate:[NSDate date]]];
NSLog(#"%#",result);
So replace
"MM/dd/yy hh:mm a"
fomatter by
"MM/dd/yy 'at' hh:mm a"
NSDateFormatter *dFormatter = [NSDateFormatter new];
[dFormatter setDateFormat:#"MM/dd/yy hh:mm a"];
NSDate *date = [dFormatter dateFromString:dateStr];
[dFormatter setDateFormat:#"MM/dd/yy 'at' hh:mm a"];
NSString *dateStr = [dFormatter stringFromDate:date];

how to show time 08:00:00 to 8 am using swift

I'm receiving time in string as 08:00:00 and I need to show it as 8 am.
I have no idea how to do it so guys help me.
I don't know how to do it using NSDate.
do like
Objective-C
NSString *sample = #"08:00:00";
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"]; // or use hh:mm:ss
NSDate *date1 = [dateFormatter dateFromString:sample];
[dateFormatter setDateFormat:#"HH a"]; // or use hh
NSString *finaldate = [dateFormatter stringFromDate:date1];
NSLog(#"%#",finaldate);
Swift
var sample: String = "08:00:00"
var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm:ss" // or use hh:mm:ss
var date1: NSDate = dateFormatter.dateFromString(sample)
dateFormatter.dateFormat = "HH a" // or use hh
var finaldate: String = dateFormatter.stringFromDate(date1)
NSLog("%#", finaldate)
Time Format
// 24 hours fomat
HH -- 13, 14, etc
// 12 hours format
hh -- 01, 02, etc

Date formate to convert NSString (Wed Jul 29 14:46:11 +0000 2015) to NSDate in ios

I have date in NSString format as "Wed Jul 29 14:46:11 +0000 2015". I have tried all possible ways of converting this string into NSDate. But couldn't find the right format for type of string.
Can someone help out or give suggestion to convert this into NSDate.
Below is the code:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = #"EEEE MM dd HH:mm:ss Z YYYY";
//[format setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:#"en_US_POSIX"];
[format setLocale:locale];
NSDate *date = [format dateFromString:#"Wed Aug 19 17:36:46 +0000 2015"];
NSLog(#"converted date is %#",date);*
But its returning wrong date as converted date is 2014-12-24 17:36:46 +0000
Thanks.
You need to set your NSDateFormatter's dateFormat to EEEE MM dd HH:mm:ss Z YYYY.
You can use the following code for converting the specified string to date.
Objective C:
NSString *string = #"Wed Jul 29 14:46:11 +0000 2015";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = #"EEEE MMM dd HH:mm:ss Z yyyy";
NSDate *date = [format dateFromString:string];
Swift:
var dateStr = "Wed Jul 29 14:46:11 +0000 2015"
var formatter = NSDateFormatter();
formatter.dateFormat = "EEEE MMM dd HH:mm:ss Z yyyy"
var date = formatter.dateFromString(dateStr)
Are you sure that:
you have the right format for NSDateFormatter when calling dateFromString()?
you have the right locale?
you have looked up any errors returned?
Please post the code and your failure message/ results.
See documentation and related SO post
You can follow this. It helps you.
NSString *dateString = #"Wed Jul 29 14:46:11 +0000 2015";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE MMMM dd yyyy HH:mm:ss Z yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];

Resources