I want to be able to display the system timezone as separate plain text in my app when it launches:
- (void)viewDidLoad {
[super viewDidLoad];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString* date = [dateFormatter stringFromDate:[NSDate date]];
lblDate.text = date;
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString* zone = [gmt:[NSTimeZone defaultTimeZone]];
txtTZ.text = zone;`
My app can display the date correctly but crashes upon the timezone. Any suggestions?
Try replacing this:
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString* zone = [gmt:[NSTimeZone defaultTimeZone]];
txtTZ.text = zone;
With this:
NSString *zone = [[NSTimeZone systemTimeZone] abbreviation];
txtTZ.text = zone;
Related
Hello I'm Beginner in ios I Have create Custom UIDatePicker View This show right date and time but when we click on done button then this show date right but time is not right ....So how do this ....
UIDatePicker *DatePicker;
UITexField *DateTime;
if ([[UIScreen mainScreen] bounds].size.height >480)
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,350,320,50)];
}
else
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,270,320,50)];
}
DatePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin;
DatePicker.datePickerMode = UIDatePickerModeDateAndTime;
DatePicker.date = [NSDate date];
[self.DateTime setInputView:DatePicker];
DatePicker.userInteractionEnabled=YES;
[self.view addSubview:DatePicker];
UIDatePicker *picker = (UIDatePicker*)self.DateTime.inputView;
self.DateTime.text = [NSString stringWithFormat:#"%#",picker.date];
NSLog(#"date picker is %#",self.DateTime.text);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss +HHmm"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *myDate = [dateFormatter dateFromString:[NSString stringWithFormat:#"%#",DateTime.text]];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
NSDateFormatter *anotherDateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setAMSymbol:#"AM"];
[dateFormatter setPMSymbol:#"PM"];
[anotherDateFormatter setDateFormat:#"dd/MM/yyyy hh:mm a"];
NSLog(#"%#", [anotherDateFormatter stringFromDate:myDate]);
DateTime.text =[anotherDateFormatter stringFromDate:myDate];
NSLog(#"datetime is %#",DateTime.text);
this picker showh right time and date in UI but when we click done button(not mention here)then it show .... DateTime.text=date picker is 2013-07-03 05:22:57 +0000 after this format it shows 2013-07-03 12:00:57 +0000
03/07/2013 12:00 AM
datetime is 03/07/2013 12:00 AM
When we Select any time then this show only above time ....Date is right ...but time is wrong so ....solve this problem
this code may be help you
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
//[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:#"dd-MM-yyyy hh:mm"];
strDatetime = [formatter stringFromDate:picker.date];
[datetimeLbl setText:strDatetime];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"HH:mm"]; //24hr time format
NSString *dateString = [outputFormatter stringFromDate: picker.date];
NSLog(#"%#",dateString);
It will print the proper time.
This may be an issue of NSTimeZone.
You can try this
NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[df_local setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];
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
HI i need to increment 15 minutes in time intrval which i get in the webservice .
The code is as follows
NSString *dateString =obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:#" %# +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDate *scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter release];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *strTime = [dateFormatter1 stringFromDate:scheduledForYellow];
[dateFormatter1 release];
NSArray *aryy = [strTime componentsSeparatedByString:#" "];
NSLog(#"end time ======>>>%#",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
Here to add 15 minutes to my start time i have written the above code , but in end time that is [aryy objectAtIndex:1] does not give me the correct time.What i wanted to do is increment 15 minutes for whatever date i get in start date .Dont know whats the issue is.
`
It must be because of the default timezone (local timezone) that the date formatter uses to generate the string. I have modified your code a bit to reuse the date formatter and fixed a leak with dateFromString.
NSString *dateString = obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:#" %# +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate * dateFromString = [dateFormatter dateFromString:dateString];
NSDate * scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString * strTime = [dateFormatter dateFromString:scheduledForYellow];
NSArray * aryy = [strTime componentsSeparatedByString:#" "];
NSLog(#"end time ======>>>%#",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
This should fix the issue. Let me know if you face any issues.
I use date picker and I set its mode to be
pickerView.datePickerMode = UIDatePickerModeDate ;
when select august 3o, 2011
to display It I use
NSDate * selected = [pickerView date];
NSString * date = [selected description];
DateLabel.text = date;
the output is 2011-08-29 21:00:00:00 + 0000
any suggestion on how to display the correct date only
Have a look at the NSDateFormatter class.
try using:
NSDate *selected = [pickerView date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:kCFDateFormatterNoStyle];
[formatter setDateStyle: kCFDateFormatterShortStyle];
NSString *date = [formatter stringFromDate:selected];
[formatter release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"DD/MM/yyyy"];//you can change format here #"MM/yyyy"
DateText.text = [dateFormatter stringFromDate:datePicker.date];
[dateFormatter release];
I currently have an API call returning me a date/time in the format: 2010-10-10T07:54:01.878926
Can I assume this is GMT? I also converted this to an NSDate object. Is there a way to use the local iPhone time to recalculate the time?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
This code will convert the GMT time to the device's local time.
NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:pubDate];
NSDate *sourceDate = [NSDate dateWithTimeIntervalSince1970:gmtTimestamp];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [destinationTimeZone secondsFromGMTForDate:0];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
[dateFormatter setTimeZone:destinationTimeZone];
NSString *localTimeString = [dateFormatter stringFromDate:destinationDate];
NSDate *now = [NSDate date];
NSLog(#"%#", [now dateWithCalendarFormat:#"%Y-%m-%d %H:%M:%S"
timeZone:[NSTimeZone timeZoneWithName:#"GMT"]]);