How to set date format retrieved from UIDatePicker - iphone

I am retrieving date from UIDatePicker ,now i want to convert in to wed, Dec29 12:30pm
format ,
I also want to set that date to UIPickerView's current display date

You can use this code,its exactly what you want
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[NSTimeZone resetSystemTimeZone];
NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:gmtZone];
[dateFormatter setDateFormat:#"day, MMM HH:mm:ss Z"];
NSDate *dateT = [dateFormatter dateFromString:str1];
NSString *strDateTaken=[appDelegate convertDate:dateT withFormat:#"EEEE, MMMM dd,yyyy hh:mm a"];
[dateFormatter release];
- (NSString *)convertDate:(NSDate *)date withFormat:(NSString *)format {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
NSString *dt = [formatter stringFromDate:date];
[formatter release];
return dt;
}
str1 is the date fetched in default format...
Have fun!

if you use jquery:
var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' });
More general info available here:
http://docs.jquery.com/UI/Datepicker#option-dateFormat
http://docs.jquery.com/UI/Datepicker/formatDate
regards
magna

Related

NSDateFormatter is returning null date

Below is my code
NSString *dateandtime =[NSString stringWithFormat:#"%#",self.eventtime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateandtime];
if i print my self.event name i am getting the output as Mon, 7 Jan 2013 15:30:00 CST but when i pass this string in NSDateFormatter i am getting null value i don't know why i tried many combinations of date formats, but i cant solve it, can anyone help me
i solved my problem as Per Prateek comments, now its working for me...Thanks Prateek.
in your self.event, you are getting CST, it should be time zone for example +0530
Use :
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
Use this code...
- (NSString *)formattedDate:(NSString*)dateSte {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[NSTimeZone resetSystemTimeZone];
NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:gmtZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateSte];
[dateFormatter setDateFormat:#"EEE,MMM d,''yyyy"];
NSString *strDate = [dateFormatter stringFromDate:date];
return strDate;
}
Use the below method for getting the NSDate from NSString date
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [self convertStringToDate:dateandtime];
Paste this below method in your .m file...
-(NSDate *)convertStringToDate:(NSString *) date {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss 'CST'"];// set format here format in which the date should be
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
For More Information About NSDate component see this post of My Blog NSDate-formate-info-for-iphone-sdk

convert NSstring date to UTC date

i need to convert some date that i have in NSString to NSString in this format: yyyy-MM-dd HH:mm:ss zzz
i write this code:
date input: date ---> 27/03/2012
-(NSString *)setDateStringFormat:(NSString *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
//[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *dateFormatted = [formatter dateFromString:date];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
return [formatter stringFromDate:dateFormatted];
}
output: 2012-03-27 00:00:00 GMT
as you all can see i got the date in GMT and not in UTC
some one have an idea?
try this method..
-(NSString *)getUTCFormateDate:(NSString *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
[dateFormatter release];
return dateString;
}
i think, Here GMT show Standard time and GMT and UTC both are same.
please look on below links:
http://wwp.greenwichmeantime.com/
http://www.worldtimeserver.com/convert_time_in_UTC.aspx
thanks

NSDate from string

I have a string "2012-09-16 23:59:59 JST"
I want to convert this date string into NSDate.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 JST"];
NSLog(#"%#", capturedStartDate);
But it is not working. Its giving null value. Please help..
When using 24 hour time, the hours specifier needs to be a capital H like this:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
Check here for the correct specifiers : http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
However, you need to set the locale for the date formatter:
// Set the locale as needed in the formatter (this example uses Japanese)
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ja_JP"]];
Full working code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ja_JP"]];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 JST"];
NSLog(#"Captured Date %#", [capturedStartDate description]);
Outputs (In GMT):
Captured Date 2012-09-16 14:59:59 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 GMT-08:00"];
NSLog(#"%#", capturedStartDate);
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// end
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

dateFromString returns nil

This is my NSString,
myDate : 2011-06-07 11:23:47 0000
And this is the code,
NSTimeZone *currentDateTimeZone = [NSTimeZone defaultTimeZone];
NSDateFormatter *currentDateFormat = [[NSDateFormatter alloc]init];
[currentDateFormat setTimeZone:currentDateTimeZone];
[currentDateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *tempDate = [currentDateFormat dateFromString:second];
Still tempDate gets the value nil.
Can anyone help?
How did you get the date string? It needs to be 2011-06-07 11:23:47 +0000 for this to work.
Try this,
This is the function to get Date from String .
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:#"EEE, dd MMM yy HH:mm:ss"];
NSDate *convertedDate = [df dateFromString:stringDate];
[df release];
// [convertedDate description]
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setAMSymbol:#"AM"];
[formatter setPMSymbol:#"PM"];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:a"];
return [formatter stringFromDate:convertedDate];
What I think is, it not related to TimeZone but the device 24hours settings.
Developer has to check if the device timings are of 24hours then they should use HH, and if the device timings are of 12hours then they should use hh.
NSTimeZone *currentDateTimeZone = [NSTimeZone defaultTimeZone];
NSDateFormatter *currentDateFormat = [[[NSDateFormatter alloc]init] *autorelease*];
[currentDateFormat setTimeZone:currentDateTimeZone];
[currentDateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss *ZZZZ*"];
NSDate *tempDate = [currentDateFormat dateFromString:second];
Add autorelease and ZZZZ insead of Z.

Manage date with NSDateFormatter

I try to get and manage date with NSDateFormatter but how to make it works.
I think it's a problem with my first "dateFormatter" which have not the right format, because when I use the second formatter with function "stringFromDate" and paramter "[NSDate date]" it works well, but not when I use my NSDate "dateFromString".
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:#"to-date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy"];
//NSString *toDate = [dateFormatter stringFromDate:dateFromString];
NSString *toDate = [dateFormatter stringFromDate:[NSDate date]];
Thank you for your help :)
Well of course, the DateFormat is not the same as the one you are receiving from the server.
Try this:
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:[dateString stringByReplacingOccurrencesOfString:#"Z" withString:#" +0000"]];
Assuming that to-date is a string and not already an NSDate try this instead:
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:#"to-date"];
NSDate *dateFromString = [[NSDate dateFromString:dateString];
If you want to display a date to the user formatted in a particular format you can then use set up an NSDateFormatter and use stringFromDate: to get a string to show the user. If it still doesn't display correctly set the calendar (I forget if you always have to set it or if there is a default).