NSDate format issue - iphone

Here is the code from the nsdate formatter... for some reason the value dateSelected is incorrect... instead of "April 30 2011 7:55PM" it returns 2011-05-01 02:55... any idea what am i doing wrong?
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"h:mm a"];
objEventInsert.eventtime = [outputFormatter stringFromDate:self.datePicker.date];
NSLog(#"%#",objEventInsert.eventtime);
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:#"LLLL d y h:mm a"];
NSDate *dateSelected = [dateForm dateFromString:[NSString stringWithFormat:#"%# %#",objEventInsert.eventstartdate,objEventInsert.eventtime]];
NSLog(#"%#",objEventInsert.eventstartdate);
objEventInsert.date = dateSelected;
NSLog(#"%#",objEventInsert.date);
NSLog response...
2011-04-30 19:54:14.264 APP[24017:207] 7:55 PM
2011-04-30 19:54:16.216 APP[24017:207] April 30 2011
2011-04-30 19:54:17.654 APP[24017:207] 2011-05-01 02:55:00 +0000

That's the correct UTC time. You'll need to set the locale/timezone to get the local time, i.e. 7:55.
See these answer examples
Inconsistent behaviour with NSDateFormatter on two different devices
NSDate dateFromString, how to parse 'around' UTC, GMT and User locale?

Your problem is that you create a new NSDate again and you just initiate it through a string. So either your should create a string in your last step or your need to reuse the NSDateFormatter.
NSString *dateSelected = [NSString stringWithFormat:#"%# %#",objEventInsert.eventstartdate,objEventInsert.eventtime];
NSLog(#"%#", dateSelected);
Note: can use appendStringByFormat to make your code less verbose.

Related

How to display date format like "Today, 25 Mar 2014 15:18" in iOS?

I want to display date like Today, 25 Mar 2014 15:18, if the date is today's date.
Also expecting Tomorrow, 26 Mar 2014 15:18, if it is tomorrow's date and Wednesday, 27 Mar 2014 15:18, if it is day after tomorrow's date.
Anyone please help me.
Sample from developer.apple.com
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:frLocale];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*1];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
NSDateFormatter *detailFormatter = [[NSDateFormatter alloc] init];
[detailFormatter setLocale:frLocale];
[detailFormatter setDateFormat:#"dd MMM yyyy HH:mm"];
NSString *detailString = [detailFormatter stringFromDate:date];
NSLog(#"detailString: %#", detailString);
NSString *finalString = [NSString stringWithFormat:#"%#, %#",dateString, detailString];
NSLog(finalString);
Apple doesn't directly support using relative date combined with absolute date. What you'd have to do is to create two formatters, one with relative enabled and one without. Then compare the output strings (e.g. "Today" and "Mar 25") and if they are not equal, combine them: "Today, Mar 25", but if they are equal, just use one.
And as an aside, actually on "après-après-demain", I think you may have found a bug (either in docs or code). Do you actually set "après-après-demain"? I tried Apple's sample code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"fr_FR"];
[dateFormatter setLocale:frLocale];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
Which Apple says produces output of dateString: après-après-demain no matter what your language is set to (as it switches the date formatter to French).
But when I ran the code on both iOS and OS X, I get the actual date: Mar 28 (and if I change to *2, I get après-demain.
So, if you're seeing the same, I'd suggest filing a bug report with Apple.

TimeZones in specific format

I am new to the concept of TimeZone in iphone.I have struck with small doubt,how can we get all the timezones in the iphone device in the format of (GMT +5:30) Bombay,Calcutta,Madras,New Delhi.
I am trying this by using the api [NSTimeZone knownTimeZoneNames] and place it in an array but it shows in different format but i want the above format.can you please help any body if you know this..
Dude use my code as i shown below. It's need some workaround but you will get what you want.
NSArray *temp = [[NSArray alloc]init];
temp = [NSTimeZone knownTimeZoneNames];
for (id tstring in temp)
{
NSString *tstring2 = [[NSString alloc]init];
tstring2 = tstring;
NSDate *now = [NSDate date];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeStyle:NSDateFormatterLongStyle];
[df setTimeZone:[NSTimeZone timeZoneWithName:tstring2]];
NSString *alltime = [df stringFromDate:now];
NSLog(#"%# %#",tstring2, alltime);
[tstring2 release];
}
>> OutPut
You will get country data but here i post for some of them only
Asia/Katmandu 4:21:21 PM GMT+05:45
Asia/Kolkata 4:06:21 PM GMT+05:30
Asia/Krasnoyarsk 6:36:21 PM GMT
America/Tortola 6:36:21 AM AST
America/Vancouver 3:36:21 AM PDT
America/Whitehorse 3:36:21 AM PDT
Europe/Copenhagen 12:36:21 PM GMT+02:00
Europe/Dublin 11:36:21 AM GMT+01:00
Europe/Gibraltar 12:36:21 PM GMT
Hope, this will help you..

NSDateFormatter ignores 24hour time format

I have been sitting with this for quite sometime now. I'm tot able to figure out, what I am doing wrong.
NSString * dateString = #"2011-11-21 13:00";
NSDateFormatter *serverDateFormatter = [[NSDateFormatter alloc] init];
[serverDateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *serverDate = [serverDateFormatter dateFromString:dateString];
NSLog(#"%#", serverDate);
Output :
2011-11-21 12:00:00 +0000
Why does 13:00 get converted to 12:00? What am I doing wrong?
You need to set the time zone, example
NSString * dateString = #"2011-11-21 23:20";
NSDateFormatter *serverDateFormatter = [[NSDateFormatter alloc] init];
[serverDateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[serverDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *serverDate = [serverDateFormatter dateFromString:dateString];
NSLog(#"%#", serverDate);
OUTPUT:
2011-11-23 14:22:10.924 xxx [23235:207] 2011-11-21 23:20:00 +0000
I think you might find the answer in this discussion. Apparently NSDateFormatter likes to use the user's settings, which may not be appropriate.
http://developer.apple.com/library/ios/#qa/qa1480/_index.html
Though I think this answer is for stopping 24-hour behavior, which is what you want to force.
I'd say you want to get the setLocale part right.
ETA: Here's some more info on this:
https://stackoverflow.com/q/7538489/290072
This will allow your code to detect whether the device will try to do 12hr or 24hr times.
What if you output via [serverDateFormatter stringFromDate:serverDate]; ?

Problem in setting nsdate value from a string

NSDate *My_StartDate,*My_EndDate ;
NSDateFormatter * df= [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
My_StartDate = [df dateFromString:#"01/05/2010 10:15:33"];
My_EndDate = [df dateFromString:#"01/05/2010 10:45:33"];
NSLog(#"%#",My_StartDate);
NSLog(#"%#",My_EndDate);
In the log i get something like this for the my_startdate as 2010-05-01 04:45:33 +0000 and end date as 2010-05-01 05:15:33 +0000 instead i should have got value as for start date as 2010-05-01 10:15:33 +0000 and end date as 2010-05-01 10:45:33 +0000
Try with below function:
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm:ss"];
//[timeFormat setDateFormat:#"HH:mm"];
//[timeFormat setDateFormat:#"HH:mm a"];
////
NSString *theDate = [dateFormat stringFromDate:dateValue];
NSString *theTime = [timeFormat stringFromDate:dateValue];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
return theDate;
}
Change Format of data as per your need.
Let me know in case of any difficulty.
Cheers.
This shows date which follow American standard time string but by this reason you don't get any problem in making your logic.Also
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
this format using 12 hour format (means 2:03 pm and 2:03 am) and date object never use am and pm for showing date object value but when you convert it correctly then it gives you right date and time.
If you feel you get any problem then use different locale for that.
It is displaying asper the GMT+4.30 time.It displays like that only.When you are converting that date to string using the DateFormatter it gives the same date(Whichever you want like start date as 01/05/2010 10:15:33 and end date as 01/05/2010 10:45:33).
NSDateFormatter * dateformatter= [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSString *dat = [dateformatter stringfromDate:My_StartDate];
then you will get the output as 01/05/2010 10:15:33
You might want to set the time zone of the date formatter to GMT here. Do it using
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
before you do dateFromString: calls. This will give you what you want.
Just need to update here in your code:
I might be like that your time would be in 24 hours format, so at that time you need to use this ....other than that you need to set the timezone.
Follow this link for All zone : http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
to
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
You are Done ;)

How I can set a date string from twitter to a NSDATE

The date you get back from twitter is in this format Fri Aug 07 12:40:04 +0000 2009. I am able to assign the value to a NSDate without issue. However, when I attempt to use NSDateFormatter, I get a nil returned to me. What am I missing?
NSDate *createdAt = [messageData objectForKey:#"created_at"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"M/d/yy HH:mm"];
NSString *dateString = [format stringFromDate:createdAt];
label.text = dateString;
i had the same question, and i could not resolve it with the current above answers. so here is what worked for me:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//Wed Dec 01 17:08:03 +0000 2010
[df setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
NSDate *date = [df dateFromString:[[tweets objectAtIndex: storyIndex] objectForKey: TWITTER_CREATED_AT_JSON_KEY]];
[df setDateFormat:#"eee MMM dd yyyy"];
NSString *dateStr = [df stringFromDate:date];
where tweets is an NSMutableArray filled with NSDictionary objects, storyIndex being the row int value (in the tableview), and TWITTER_CREATED_AT_JSON_KEY being a constant NSString with value created_at. use the dateStr wherever you wish to display the date
If the object associated with the #"created_at" key is a valid NSDate object, this code should work.
However, I'm guessing that it is actually an NSString. If so, it will produce the behavior you're describing.
If I'm right, the code snippet above is assigning an NSString object to an NSDate reference. NSDictionary returns untyped 'id' objects, so the compiler won't give you a type mismatch warning.
You'll have to use NSDateFormatter to parse the string into an NSDate (see dateFromString:).
It looks likes, Twitter changes the format again and again. These works for me now. Don't forget to set the locale.
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//Mon, 29 Oct 2012 12:24:50 +0000
[df setDateFormat:#"EEE, dd LLL yyyy HH:mm:ss ZZZ"];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
NSDate *date = [df dateFromString:[element valueForKey:#"created_at"]];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *dateStr = [df stringFromDate:date];
[element valueForKey:#"created_at"] - date from twitter
Above nothing worked for me - try this for Twitter: (I've also included a nice nsdate category for better showing date
lbl = (UILabel *)[cell viewWithTag:16];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//"created_at": "Tue Jul 09 07:45:01 +0000 2013",
[df setDateFormat:#"EEE MMM d HH:mm:ss ZZZZ yyyy"];
NSTimeInterval timeIntveral = [[df dateFromString:[[_twitterArray objectAtIndex:indexPath.row] objectForKey:#"created_at"]] timeIntervalSince1970];
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:timeIntveral];
//will work if you added the nsdate category, linked above
NSString *ago = [date timeAgo];
NSLog(#"Output is: \"%#\" %.f", ago,timeIntveral);
lbl.text = ago;
Hope this helps!
First off, what are you using stringFromDate: for? That's if you already have an NSDate and want to make a string representing it.
Moreover, when you do use the date formatter, you're giving it a format string that doesn't match the date string you're trying to interpret.
Change the format string to match your date strings, and use dateFromString: instead of stringFromDate: (with the attendant changes to your variable declarations), and it should work.