I'm working on an application in which user set a time to do a task and before 10 or 15 minutes application triggers an alarm. Just like a reminder app you can say.
After searching a lot on stackOverFlow and other different links my problem still exists. Here is what I'm doing:
-(void)timeWasSelected:(NSDate *)selectedTime element:(id)element {
if((UITextField *)element == self.timeForTask){
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
self.textField.text = [dateFormatter stringFromDate:selectedTime];
[defaults setObject:self.textField.text forKey:#"taskTime"];
myTime = [selectedTime dateByAddingTimeInterval:-600];
NSLog(#"New Time %#",myTime);
}
After doing this the NSLog part gives this output: New Time 2015-04-27 06:14:14 +0000
The problem is that I've selected time 11:22AM and it is storing 06:14 and I've only deducted 10min from the selected time.
Try this you will get output:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate* selectedTime = [dateFormatter dateFromString:#"11:22 AM"];
NSDate* myTime = [selectedTime dateByAddingTimeInterval:-60*10];//10 minute deduction
NSString* updateTime = [dateFormatter stringFromDate:myTime];
NSLog(#"New Time %#",updateTime);
Output :
New Time 11:12 AM
Here you are subtracting 10 Minutes from selected time. if you want 15 Minutes then replace with 10 digits.
Hope this help you.
Try this, may help you,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSLog(#"1New Time %#",[dateFormatter stringFromDate:selectedTime]);
self.textField.text = [dateFormatter stringFromDate:selectedTime];
[defaults setObject:self.textField.text forKey:#"taskTime"];
NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:destinationTimeZone];
NSDate *myTime = [selectedTime dateByAddingTimeInterval:-600];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setTimeZone:destinationTimeZone];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd hh:mm:ss ZZ"];
NSLog(#"2New Time %#",[dateFormatter1 stringFromDate:myTime]);
Enjoy Coding !!
// gives new date object with time 15 minutes earlier
NSDate *newDate = [oldDate dateByAddingTimeInterval:-60*15];
NSString* NewTime = [dateFormatter stringFromDate:newDate];
NSLog(#"New Time %#",newDate);
This code will exactly get you 15 minutes earlier time
Related
So i am getting a string containing date and time in this format "2014-12-22T11:00:00+0500" Now in order to convert it into NSdate i am using
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate* date = [dateFormatter dateFromString:start_time];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString* temp = [dateFormatter stringFromDate:date];
self.eventDate = [dateFormatter dateFromString:temp];
NSDateFormatter* timeFormatter = [[NSDateFormatter alloc]init];
[timeFormatter setDateFormat:#"HH:mm:ss"];
NSString* temp2 = [timeFormatter stringFromDate:date];
self.start_time = [timeFormatter dateFromString:temp2];
Now even though the conversion is successful the problem is that eventDate also has has time after date 00:00:00. How can i remove this so that eventDate only contains date.
Conversly start_time has the time of event but also has some arbritrary reference date before that. How can i remove that so i only have time in start_time
I have searched hard and fast but haven't been able to figure out this problem. Any help would be appreciated.
You cannot remove either the date or the time to keep only one component. If I remember correctly NSDate object is internally just a number of seconds relative to a fixed point in time. So every NSDate contains the full date and time information.
What you probably want to do is to get the NSDateComponents you want from a NSDate object.
Instead of trying to store this separate, just display these dates separate. I think it could be useful sometimes to get the date completly, but i don't know your idea.
You can try with it, it may be help you.
NSString *finalDate = #"2014-12-22T11:00:00+0500";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [dateFormatter dateFromString:finalDate];
//For getting Time
NSDateFormatter* df1 = [[NSDateFormatter alloc]init];
[df1 setDateFormat:#"hh:mm:ss"];
NSString *time = [df1 stringFromDate:date];
NSLog(#"time %# ", time);
//For getting Date
NSDateFormatter* df2 = [[NSDateFormatter alloc]init];
[df2 setDateFormat:#"yyyy-MM-dd"];
NSString *actualDate = [df2 stringFromDate:date];
NSLog(#"actualDate %# ", actualDate);
It looks easy, but I couldn't figure out a proper way to do this. I need to create an NSString from a NSDate which represents the same time on every device, independently from the iPhone's time zone settings.
Suppose userA is in London, where the actual time is 14:00, userB is in New York where is 9:00 and userC is in Hong Kong, where the actual time is 21:00.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
Actually with this code I'm getting these results (when I log the dateString):
userA: 08/12/14 14:00:00
userB: 08/12/14 09:00:00
userC: 08/12/14 21:00:00
But I need to create dates like this
userA: 08/12/14 14:00:00
userB: 08/12/14 14:00:00
userC: 08/12/14 14:00:00
My goal is to create a "system/absolute time" which is the same inside the app and doesn't matter the original time zone of the user's device. The end result must look like this MM/dd/yy HH:mm:s.
Is it possible to get the NSDate *now = [[NSDate alloc] init]; from a pre-defined timezone? For example it could always use the actual time of the GMT-00 timezone.
I've tried to do it with this code, but when I run the code, the console writes out the wrong date (based on the device time zone setting) again, so I don't have a better idea. I would appreciate any ideas.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"Europe/London"];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(#"the date is %#,", dateString);
The below code should work. What ever the timezone you are in it will always display the time in UTC.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[[NSTimeZone alloc] initWithName:#"UTC"]];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(#"%#",dateString);
Between the following two threads, I think you'll find everything you need (and thensome). The first is an extensive example of a problem similar to yours (just remember to look at the code in the answers and not the question), while the second has all the time zone abbreviations that you'll ever need. Gotta love the helpful people on The Stack.
The links again, just in case
objective-c: Conversion between TimeZones
GMT timezone conversion in objective c
NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = #"HH:mm:ss";
NSString *dateString = [timeFormatter stringFromDate: localDate];
I have a time string in the following format(ISO 8601):
start = "1900-01-01T11:30:00"
stop = 1900-01-01T21:30:00
From the above string I need to display time like this :
Starting Time : 11:30a.m
Stopping Time : 09:30p.m
For that I had created an NSDate using below code, But I think it displays time in GMT format.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
date = [dateFormatter dateFromString:opening];
stopTime = date;
//Output of above code : Date = 1900-01-01 15:36:40 +0000
How can I obtain the Starting and Ending time string from above ISO 8601 time string? Do I need to depend on String operations rather than using iOS NSDateformatter?
Try this,
NSString *start = #"1900-01-01T11:30:00";
NSString *stop = #"1900-01-01T21:30:00";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *sDate = [dateFormatter dateFromString:start];
NSDate *eDate = [dateFormatter dateFromString:stop];
[dateFormatter setDateFormat:#"hh:mm a"];
start = [dateFormatter stringFromDate:sDate];
stop = [dateFormatter stringFromDate:eDate];
NSLog(#"Starting Time : %#", start);
NSLog(#"Stopping Time : %#", stop);
And the outputs will be
2014-02-12 10:55:13.931 [950:a0b] Starting Time : 11:30 AM
2014-02-12 10:55:13.932 [950:a0b] Stopping Time : 09:30 PM
Check this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *currentDate = [NSDate date];
NSString *iso8601valueStr = [dateFormatter stringFromDate:currentDate];
I need to convert time which I get from server to users time.
I use This code.
- (NSString *)convertServerTime:(NSString *)serverTime
{
NSDateFormatter *serverFormatter = [[NSDateFormatter alloc] init];
[serverFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[serverFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *theDate = [serverFormatter dateFromString:serverTime];
NSDateFormatter *userFormatter = [[NSDateFormatter alloc] init];
[userFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[userFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateConverted = [userFormatter stringFromDate:theDate];
return dateConverted;
}
system time zone: (Europe/London (GMT) offset 0)
serverTime: 2014-08-11 00:00
dateConverted: 2014-08-11 01:00
But I need to get the same time as serverTime. Instead this I receive +1 houre. Please help me.
This is really odd, the code below takes self.danceTimeIn (its text state) and converts it to an actual time. The problem is that its coming up 1 hour LESS than what's entered. Meaning that if I enter 14:03 I'll get 13:03 in the database! The same thing is happening with the date version of this code
++++++++++++++++++++++
TIME
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
DATE
NSString *danceDateValue = self.danceDate.text;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yy"];
NSDate *date = [dateFormat dateFromString: danceDateValue];
++++++++++++++++++++++
Anyone ???
Try using this. NSDateFormater change date according to locale of your device settings, if you set locale properlyl, you will get proper date. Try if this works :)
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[timeFormatIn setLocale:usLocale];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
Time zone may be causing this problem.Try
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
May be this will help.
It depends on the timezone:-
first Check your local time zone
NSTimeZone *tz=[NSTimeZone timeZoneWithName:#"GMT"];
[dateformat setTimeZone:tz];
and then set your date accordingly.