Is there an easy way with NSDateFormatter to get "Today", "Tomorrow","Friday" style dates? - objective-c

I'm working on setting up NSDateFormatter to explain the date, and I'd like something short but more intuitive than 15/07/10. I think I've seen some formats that will say simply "Today" or "Tomorrow" or the day of the week for subsequent days of the same week. Is there a simple apple-approved way to get this type of date?
Thanks.

If you're targeting iOS 4 or later, you can call [yourFormatter setDoesRelativeDateFormatting:YES]. Otherwise, you'll most likely need a custom subclass. As for days of the week, what jer said.

Ensure you set up your locale with the NSDateFormatter appropriately, and then ask for the weekdaySymbols. This will return an array with the days of the week in the locale you specify.

Related

Parse check if object was created today? Objective-C

I am trying to write a PFQuery for my application that returns only objects that were created today. With all the depreciations that happened to NSDate and the growing importance of NSDateFormatter I am finding rather hard to go about this.
I have figured the logic to be like this in pseudo-code:
Query q = new Query();
q.whereDateGreaterThan(midnightThisMorning);
q.whereDateLessThan(midnightTonight);
I can't seem to figure out how to get a NSDate object set to 12:00 AM (which would be midnightThisMorning) and another set to 11:59 PM tonight (which would be midnightTonight).
Check out NSCalendar's startOfDayForDate method to find the start of the current day.
To find the end of the current day, you'll want to add a day to the date returned from startOfDayForDate and then subtract one second.

convert json utc "/Date(1420095600000-0700)/" date to nsdate

I get date like "/Data(xxx....xxx-xxxx)" from api. How can i get nsdate form this format. Temporarily I solved this problem to truncate the string and get seconds. but I need proper solution for this problem. kindly help me
Use NSDateFormater to transform this String (the parts within the brackets) into an NSDate and vice versa. See the docs here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/occ/instp/NSDateFormatter/dateFormat and here about the various formating possibilities: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/DataFormatting.html#//apple_ref/doc/uid/10000029i
Well, you may have to get rid of the milliseconds.

Objective-C format date and string

guys
I have two question to ask, they're easy, but bothering me for a while.
I access my .NET test WebService, and it return two parameters to me.
One is a date data just like "/Date(1332399761677+0800)/", and I dont know
how to format it to the normal date format.
Two is a NSString data looks like "12000.00000",and I want to change it to
the format like this:"12000.00".
So,please help me with this two problems. Thank you in advance.
1332399761677 looks like a Unix date, so if you grab that part of the string, use NSString doubleValue to turn it into a double, then use NSDate dateWithTimeIntervalSince1970:, you should be able to get a date. +0800 looks like a timezone, but you wouldn't need the timezone to get the date given a Unix date: 1332399761677 would specify a specific point in time, irrespective of timezones.
As for "12000.00000", you would use doubleValue to make it into a double, make an NSNumberFormatter with its maximumFractionDigits and minimumFractionDigits set to 2, then use stringFromNumber:.

CFAbsoluteTimeGetCurrent and DST

I'm developing an app for iPhone that uploads pictures to a webserver. These pictures have the time of when they were taken in the filename. Since they can be taken from anywhere in the World, I have to keep attention to timezones and DST. I thought I can use CFAbsoluteTimeGetCurrent, that shouldn't be localized ([NSDate date] returns the localized version of time, with or without "AM", "PM", "p.m." or any other variant... for example it returns Arabian characters, if your phone is set in arabic language!).
So, can you suggest me a function to convert CFAbsoluteTimeGetCurrent to a "MySQL like" date, something like "2011-11-03 14:12:10"?
My second question is: what about timezones and daylight saving? CFAbsoluteTimeGetCurrent always returns an UTC date, no matter how the iPhone timezone is set? Is it always DST-free?
Of course I know that the local iPhone date/time could be wrong, but milliseconds-precision is not important for my application :)
Thank you in advance!
What on earth do you mean by
([NSDate date] returns the localized version of time, with or without "AM", "PM", "p.m." or any other variant... for example it returns Arabian characters, if your phone is set in arabic language!).
[NSDate date] returns an NSDate* object. I'm assuming what you really mean is the output of -[NSDate description] returns a string localized in the user's current locale, but then the question is, why are you depending on the output of -[NSDate date]? If you need to format a date a certain way, you should use an NSDateFormatter.
CFAbsoluteTime represents a moment in time, independent of time zones or localization settings. If you want to format a date (i.e. an NSDate object) you should look at NSDateFormatter, which lets you specify the exact format and localization (if any) of the output string. (If you need to work at the Core Foundation level, CFDateFormatter does the same thing.)

Change UIDatePicker from 12 hour clock to 24 hour clock and back

I'm sorry to make my first question here a bit of a simple one -- I've spent a day reading the NSLocale and NSCalendar class descriptions but I couldn't see if this was possible.
I have a UIDatePicker in the UIDatePickerModeDateAndTime mode. It is currently displaying date and time according to the user's locale, which is the default behavior.
I would like to be able to offer the option to show the UIDatePicker in either 12-hour or 24-hour time format. Detecting which time format the user is currently using isn't a problem, but I'm not clear on how to change just the time format of UIDatePicker without entirely throwing out the user's locale settings (since the picker also displays the localized days of the week and months). UIDatePicker supports setting its locale and setting its calendar.
So, question one is whether this is something I should be trying to do via NSLocale or NSCalendar, and question two is if anyone can recommend a way to isolate the time format without throwing out the rest of the user's locale settings.
Thanks for any suggestions.
This is not the answer you are looking for, but in Cocoa you could create an NSDateFormatter and attach it to an NSDatePicker (which is an NSControl) using setFormatter. Unfortunately the equivalent iPhone class (UIControl) does not support this yet. I raised a bug with Apple about it and this is a known issue, although they wouldn't tell me if/when they plan to fix/enhance it.
OK, after reading the last two years' worth of others asking the same question here, it seems that there is no way to do this since the UIDatePicker uses the user's country setting instead of the user's locale setting, and that can't be overridden programmatically. I filed a bug.
I have solved this problem by using the user input of AM/PM by initializing another variable as a string of either AM or PM based upon the 12 hour format. The new hour in 24 hour format is put into NSCalendar. To wit:
var hra = 1//ENTER INITIAL HOUR (HH)
var dna = "PM"//ENTER "AM" OR "PM"
if hra <= 12 && dna == "PM"{
hra = hra + 12
}
The variable hra now takes the value of the 24 hour format.