I have some data which contains sold date. I want to sort them according to Week, Month, Year. For eg. If I select week then results returned should be within 7 days of current date. Same goes for Month and Year.
The relevant field from the data which is fetched from a web service looks like the following:
AuctionStartTime = "05/03/2016 09:30:00 AM"
You have use NSDateFormatter to convert the string to your date format and using NSDateFormat to get required date values and perform your task. The set date format using following code...
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"Your Date Format"];
Convert string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateStr];
The your date format to set your required format. For more details of date format to click now.
You shoud use a NSDateFormatter to convert the NSString date to NSDate then you can compare that to current date. After that you can easily pick the last week's, month's or year's dates.
Related
I have a date picker in my app. The phone is set to Bangladesh local settings. When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.
Like, it returns ০৬/১১/২০১৪
but I want it to be 06/11/2014.
I've tried converting it by date formatter. This is what I tried:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: self.birthDate.text];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US"]];
[formater setDateFormat:#"dd/MM/yyyy"];
NSLog(#"%#",[formater stringFromDate:date]);
The output is null.
You are incorrect in your assumption when you say...
When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.
UIDatePicker returns an NSDate object. NSDate has no formatting at all. It has no language, it is purely a point in time.
When you do this...
NSLog(#"%#", someDate);
The system will render that point in time into a string and then print it. It is the rendering into a string that contains the format.
I'm guessing what you are doing is this...
Get a date from a UIDatePicker.
Render that date into a UITextField in Bengali. (or label, or text view or something)
Trying to read the text and store it into a date.
Trying to then "convert" the date to an English string.
What you should be doing is just saving the date that comes from the date picker.
Put it into a property or something.
In your above code I think the bit that is failing is actually the first bit...
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: self.birthDate.text];
Because you're not giving it a format it will fail. But this is the wrong way to go about it anyway.
You should have something like this...
- (void)datePickerChoseADate
{
self.date = self.datePicker.date;
}
I am new in iOS development. Actually I am trying to show some information which I get from a web service in a table view.
I have successfully retrieved the response as JSON in my code, but in my table view there is a label to show date.
But in my response the date is something like this
/Date(1391068800000)/
How can I convert this to show in my table view? I think this is Javascript date.
But I'm not sure how to convert it.
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[your timestamp doublevalue]/1000];
divide value by 1000 cause of milliseconds (13 digit)
The date in your response is a timestamp. You can create a date object by:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1391068800000];
After that you can convert this date to a string in an appropriate format by using the NSDateFormatter class. For example:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateString = [formatter stringFromDate:date];
For more information about formatting dates see: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
How can I output human readable dates like turn NSDATE to human readable form like Tomorrow, one week from now, 1 month from now , etc?
For example if the current time is January 19, 2012 7 am
and the input date is January 20, 2012 9am
then the function should out Tomorrow at 9am.
I found how to do this with past dates online but not with future dates.
Also the functions I found online were not specific. Any help would be appreciated! thanks
You need to use an NSDateFormatter. From the docs:
setDoesRelativeDateFormatting: Specifies whether the receiver uses
phrases such as “today” and “tomorrow” for the date component.
- (void)setDoesRelativeDateFormatting:(BOOL)b
Parameters b YES to specify that the receiver should use relative date
formatting, otherwise NO. Discussion If a date formatter uses relative
date formatting, where possible it replaces the date component of its
output with a phrase—such as “today” or “tomorrow”—that indicates a
relative date. The available phrases depend on the locale for the date
formatter; whereas, for dates in the future, English may only allow
“tomorrow,” French may allow “the day after the day after tomorrow,”
as illustrated in the following example.
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);
// Output
// dateString: après-après-demain
I am using the below method to convert a NSString to NSDate.
Always when I construct the NSDate from String, the date is one day behind the current day I have provided as part of the input and hour is 18:30:00 +0000. Why this deviation from what I have provided. I was expecting to have the same date what I have provided and hour as 00:00:00 +0000
+(NSDate*)convertStringToNSDate:(NSString*)string withFormat:(NSString*)format{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setDateFormat:format];
NSDate *date = [dateFormat dateFromString:string];
[dateFormat release];
return date;
}
This question comes up quite regularly but I could not find a suitable duplicate (searching on the phone does not help).
NSDate represents a specific point in time. When you log the value of an NSDate it is displayed in GMT, which is 5.5 hours behind your timezone (India, I assume). So the value is correct. If you run that date back through your date formatter you will get the local time of midnight again, since the date formatter is using your local time zone.
I have data in NSString, I need to display it as Oct 3, 2011. I am having trouble in converting nsstring into NSDate and then again display it as NSString.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy "];
NSDate *dateFromString = [dateFormatter dateFromString:myString];
myString is 2011-10-3 00:00:00
First set the date formatter time zone. If the string you're receiving is GMT/UTC, set the timezone to that.
Next set the date format to match the incoming date pattern. Do dateFromString.
Then set the date format to match the desired output format. Also set the output timezone, if different. Do stringFromDate, using as input the NSDate object from the previous operation.