Current date with the selected date to be passed - iphone

I want to pass two dates 1st which i will select from the datetime picker & 2nd the current date of the iPhone then what is the process for the current date to pass to the webservice.

You can pass current date as a string through url.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yy-MM-dd"];
NSString *dateString=[formatter stringFromDate:[NSDate date]];
NSLog(#"Today is %#",dateString);

Related

Date conversion issue while setting region from settings application

I want to convert string to date. Its not working in following situation.
I am using settings application to set region. In settings, go to general, go to international, go to region format, go to spanish and then set Argentina.
After setting these parameter, I try to convert string to date. Its not converting string to date. Please help.
EDIT : This is my code.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:#"MM/dd/yyyy h:mma"];
NSDate *_date = [formatter dateFromString:strDate];
This document explains what you're experiencing
In short, format your date with the american locale, if you wish to display the date to a user, convert the date to a string.
For example:
NSString *strDate = #"05/29/2012 6:15PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:#"MM/dd/yyyy h:mma"];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
NSDate *_date = [formatter dateFromString:strDate];
And to show the date to a user:
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *newDate = [formatter2 stringFromDate:_date];
Date strings entering the application should typically be converted to a standard format, should you ever want to display a date to the user, you would need to format the date back to a string - think of them as seperate steps:
Get the string to a sane date format
Get the date to the users locale with the desired formatting
When you are converting a String to Date, the String must be in Spanish(Argentina) format for the NSDateFormatter to understand.
in your example, the date you passed is "05/29/2012 6:15PM"
However, in Spanish(Argentina), the date string should be "05/29/2012 6:15p.m."
If you are getting this date dynamically, best is to remove the AM/PM component in the date and try getting the Date String in 24-Hour format

Converting the time in datetime format in iPhone

To all:
Can someone please help me about how to convert the time (HH:mm) format to the datetime(yyyy/mm/dd HH:mm:ss) format? I need help.
I already tried this String to a Format yyyy-MM-dd HH:mm:ss Iphone link, but I am having the string in this format HH:mm and I want to convert this into the yyyy/mm/dd HH:mm:ss
Thanks in advance!!
#Neelz saying right If in DB you are saving only hours & minute value then it will be depends on you which date you are using to get the formatted date.I did the same which #Neelz said but in different way.Below I shown the code by considering the today's date to get the date time as you need
//get the today's date
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:#"en_US"]];
//Set the required date format to get the todays date
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString* cCurrentDateStamp = [formatter stringFromDate:date];
NSLog(#"%#",cCurrentDateStamp);
//now get the hours & minute you saved in DB.I considered 11:08
NSString *strYOurTime=#"11:08";
//create the date time string by appending date & time
NSString *str=[NSString stringWithFormat:#"%# %#:00",cCurrentDateStamp,strYOurTime];
//set the formatter
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *FinalDateTime=[formatter dateFromString:str];
NSLog(#"%#",FinalDateTime);
FinalDateTime is the final date which you want.Now to get the number of days between two dates refer following link:
iPhone - get number of days between two dates

Getting NSDate from NSString

I am trying to convert NSString to NSdate. The string has date in it.i use the following
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:setitempricedate];
NSLog(#"Date : %#",date);
the input string contains the date in the format 02/08/2011
when i log date , i get 2011-02-07 18:30:00 GMT,
I want to get the date as 02/08/2011 only. Where am i going wrong
In your code, you are asking the date formatter to create a date object for you from a given string. Then you printed out that date object. What you want is to create that date object, then ask the date formatter to format that date object you just created. You should be calling stringFromDate instead.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:setitempricedate];
NSLog(#"Date: %#", [dateFormat stringFromDate:date]);
You will not be able to change the date representation. These (NSDateFormatter, NSCalendar) classes are provided to get formatted strings not date.
Whenever you will have an instance of NSDate class it will be in same format like you are getting.
2011-02-07 18:30:00 GMT
If you want custom styles better you go with NSString
You are printing the actual date object which doesn't follow the date format you specified. You could do something like
[[NSDate dateWithString:setitempricedate] stringFromDate];

comparing the date in iphone

I have the date function.And I have to compare the previous date with current data in iphone
For current date I have code:-
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"yyyy-MM-dd"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(str);
//Set in the lable
[dateLabel setText:str];
But I want to implement the compare the current date & previous date in iphone
Thanks in advance
You can compare dates using NSDate's comparison methods:
- (NSDate *)earlierDate:(NSDate *)anotherDate;
- (NSDate *)laterDate:(NSDate *)anotherDate;
- (NSComparisonResult)compare:(NSDate *)other;
- (BOOL)isEqualToDate:(NSDate *)otherDate;
Check NSDate class reference to know more..

iOS SDK: How to get the date out of a UIDatePicker?

I need to retrieve the date from a UIDatePicker (Preferably I would also like to be specify the format as well. For example, mmdd would output the string 1209. Any string that reasonably parsed would work as well.
Thanks in advance.
You need to use the date property:
NSDate *myDate = datePicker.date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"cccc, MMM d, hh:mm aa"];
NSString *prettyVersion = [dateFormat stringFromDate:myDate];
BTW, it's not obvious but you can add specific non-parsed text by encompassing it inside single quotes in the format:
[dateFormat setDateFormat:#"'Game-'yyyyMMdd-HHmm'.xml'"];
NSString *filenameVersion = [dateFormat stringFromDate:myDate];