Set manual time to today date and make datetime object in ios6 - iphone

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
NSTimeZone *gmt = [NSTimeZone defaultTimeZone];
[dateFormatter setTimeZone:gmt];
NSString *cur_date= [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#",cur_date);
Now I am setting the manual time to today's date:
NSString *cur_time=#"13:05:08";
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *date_obj=[dateFormatter dateFromString:[cur_date stringByAppendingFormat:#" %#",cur_time]];
NSLog(#"%#",date_obj);
but I am getting today date in NSlog to show as
2012-09-28 07:35:08 +0000
even if I set the time as 13:05:08.
How do I set the manual time to the current date and make one datetime object from that?

//Try using Time Interval
[myDate addTimeInterval:entertimeinterval];

Do this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
//You can either use UTC or GMT
NSTimeZone *gmt = [NSTimeZone timeZoneWithName:#"GMT"];// This is where I made the change
[dateFormatter setTimeZone:gmt];
NSString *cur_date= [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#",cur_date);
NSString *cur_time=#"13:05:08";
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *date_obj=[dateFormatter dateFromString:[cur_date stringByAppendingFormat:#" %#",cur_time]];
NSLog(#"date %#",date_obj);
Hope this will work

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

How to get the localized day of an NSDate as string?

I have an NSDate object and must create a label that indicates the day of the week in a short 3-char style like 'Mon', 'Tue', 'Wed', ... and localized to the short representation in any language.
The date formatter EEE has 3 chars but how does that translate to languages like Chinese which probably only have one character?
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE"];
NSString *strDate = [formatter stringFromDate:date];
[formatter release];
//Commented out the following line as weekday can be of one letter (requirement changed)
//strDate = [strDate substringToIndex:3]; //This way Monday = Mon, Tuesday = Tue
To use the correct locale you'll want to set the dateFormat on the instance of NSDateFormatter using the + dateFormatFromTemplate:options:locale: method.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:#"EEE" options:0 locale:[NSLocale currentLocale]];
NSLog(#"Weekday using %#: %#", [[NSLocale currentLocale] localeIdentifier], [dateFormatter stringFromDate:date]);
// Test what Chinese will look like without changing device locale in settings
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"zh_CN"];
NSLog(#"Weekday using zh_CN: %#", [dateFormatter stringFromDate:date]);
This prints:
Weekday using en_US: Thu
Weekday using zh_CN: 周四
This will print what you need:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE"];
NSString *day = [formatter stringFromDate:date];
[formatter release];
if ([day length] > 3) day = [day substringToIndex:3];
NSLog(#"%#", day);

Problem formatting date using NSDateFormatter

I have the following date:
2011-09-09T18:01:47Z
I want it to appear as
"9/9/11 at 1:47PM" whatever the correct time is
This code returns a null string:
NSLog(#"TIME: %#",[message valueForKey:#"created_at"]);
NSString* time = [message valueForKey:#"created_at"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yy at HH:mm:ss"];
NSDate* newTime = [dateFormatter dateFromString:time];
[dateFormatter setDateFormat:#"MM-dd-yy at HH:mm:ss"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
Your formatting is the same for both and they have little to no relation to your input and your output. Refer to the Unicode Data Format Patterns whenever you have issues.
NSString *datein = #"2011-09-09T18:01:47Z";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:datein];
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
NSLog(#"%#", finalTime);

How to set date format retrieved from UIDatePicker

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

Crash when converting string to date

In my app I DO NOT want to use the local time on the phone. I want to get the current time in GMT. To do this I have a date formatter with time zone set to GMT. I then do the following to get back the current time:
NSString *dateOne = [df stringFromDate:[NSDate date]];
NSDate *date1 = [df dateFromString:dateOne];
My app gets an EXC_BAD_ACCESS though on the dateFromString call. I checked the returned value from dateOne and it's format matches the dateformatters. What am I doing wrong? Is there a better way to do this?
Thanks
EDIT:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-mm-dd HH:mm:ss"];
An easier way perhaps:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd HH:mm:ss"];
NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:tz];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"Time now in GMT: %#",dateString);