How to increment one second by a NSDate object - iphone

I have a NSString which have the format of #"2013-01-09 06:10:10 +0000" (I am getting it from server and it is not the current time). I want to increment it by one second continuously. I can use a timer for doing it, but how to increment the time by one second?

Try this,
NSDate *correctDate = [NSDate dateWithTimeInterval:1.0 sinceDate:yourDate];
You can get yourDate from string using NSDateFormatter.

Nowadays (2017) Apple recommends to use (NS)Calendar for all kinds of date math
Objective-C
NSDate *now = [NSDate date];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *nowPlusOneSecond = [currentCalendar dateByAddingUnit:NSCalendarUnitSecond
value:1
toDate:now
options:NSCalendarMatchNextTime];
Swift 3
let now = Date()
let currentCalendar = Calendar.current
let nowPlusOneSecond = currentCalendar.date(byAdding: .second, value: 1, to: now)!

add 1 second in your date like bellow..
NSDate *mydate = [NSDate date];
NSTimeInterval secondsInEightHours = 60; // you can add hours and minuts with multiply the numbers with this second..
NSDate *dateEightHoursAhead = [mydate dateByAddingTimeInterval:secondsInEightHours];

Say your date string is in var called serverString. You can get a date this way...
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];
NSDate *date = [df dateFromString:serverString];
And increment it this way:
date = [date dateByAddingTimeInterval:1.0];

SWIFT 3.x Solution
// Just an example to present second level date/time calculation
let time = NSDate()
let interval:Double = 5.0
let timeFiveSecondLater = time.addingTimeInterval(interval)

Related

Days Difference between current and an up coming Date in iOS

I have tried so many things through the help I got, but I still can't figure out how to do it properly. Here's what I did lastly.
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"dd-MM-yyy"];
NSDate *currentDate = [NSDate date];
NSDate *fromDate = [NSString stringWithFormat:#"%#",[tempFormatter stringFromDate:currentDate]];
NSLog(#"currentDate %#", fromDate);
NSDate *toDate = [NSString stringWithFormat:#"%#",[tempFormatter stringFromDate:datePicker.date]];
NSLog(#"toDate %#", toDate);
NSTimeInterval interval = [toDate timeIntervalSinceDate:fromDate];
double leftDays = interval/86400;
NSLog(#"Total interval Between::%g",leftDays);
Tell me what I did wrong. Is it the NSDate conversion, that I am not doing properly ??
Thanks.
Your code is all messed up -- both toDate and fromDate are strings not NSDates. Your from date should just be currentDate, and your toDate should just be datePicker.date. You don't need to do anything with converting to strings or using a date formatter to get the time interval.
This line is creating problem.
NSDate *toDate = [NSString stringWithFormat:#"%#",[tempFormatter stringFromDate:datePicker.date]];
It changes the type of toDate from NSDate to __NSCFString. The NSTimeInterval take both of its arguments of NSDate type, but in your case only fromDate is NSDate type.
Change your code with these lines
NSDate *currentDate = [NSDate date];
NSDate *toDate = datePicker.date;
NSTimeInterval interval = [toDate timeIntervalSinceDate:currentDate];
It will surely work (inshaAllah).
You're certainly on the right track; however, you seem to be calling "timeIntervalSinceDate" using two NSString's (even though you're specifying fromDate and toDate as NSDates, look right after that- you're setting those two variables to NSString objects).
To get the interval you're looking for, try:
[datePicker.date timeIntervalSinceDate:currentDate];
That should get you the right interval. In addition, you may want to change leftDays to equal
double leftDays = abs(round(interval/86400));
This will stop leftDays from being an awkward number like -1.00005.
`Passing NSString to NSDate! this code is wrong
try
NSDate *curDate = [NSDate Date];
NSDate *pickerDate = datepicker.date;
then compare both these dates using NSTimeInterval

Parse Date to timestamp

I have such response from server Date = "/Date(1348783200000+0200)/" how can I parse it to timestamp or date (example: Monday 21, September, 2012)?? please help..
It looks like your date is in milliseconds. You will need to divide by 1000 and cast it into an NSDate after that. Then you can just use NSDateFormatter with NSDateFormatterLongStyle to show the date in that format.
NSTimeInterval dateInterval = dateWithMilliseconds;
dateInterval = dateInterval / 1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateInterval];
NSDateFormatter *formatter = [NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSString *dateString = [formatter stringFromDate:date];

mktime(from php) to iphone time(string)

I have in variable value from PHP function mktime(time from epoch), exacly 133123088.
How I can change it for readable date in iphone?
Is any function to convert this format to another like NSDate or just string ?
NSDate* date = [NSDate dateWithTimeIntervalSince1970: 133123088]
NSTimeInterval timeInterval = 133123088; // NSTimeInterval is a double
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
Notice that 133123088 corresponds to a date in 1974. So I guess you are missing a digit in 133123088 to make it a date in 2012: For example : 1331230880
NSTimeInterval timeInterval = 1331230880;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy/MM/dd HH:mm:ss";
NSString *dateString = [formatter stringFromDate:date];

How to compare just dates not the time?

How can I compare the dates only, not the time. I am using
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *tempDate = #"2-2-2012"; //Dynamic Date
NSDate *dateString = [dateFormatter dateFromString:tempDate];
NSLog(#"%#",dateString);
It logs this: 2012-02-01 18:30:00 +0000
NSDate *now = [NSDate date];//Current Date
NSLog(#"%#",now);
It logs this: 2011-04-04 14:49:45 +0000
I want to compare Dynamic date and current date, I don't need time. I may not using the correct NSDateFormatter. Can anyone of you tell me how to do this? If I am not clear, please let me know.
Suppose I have to strings
date1 = 3-2-2011;
date2 = 4-5-2020;
I want to convert them in date, only after that I can compare them. Its not happening from my date Formatter. Please have a look.
Thanks!
Simplest way is to compare date by converting it into string.
Sample Code is as shown below:
//Current Date
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];
//Other Date say date2 is of type NSDate again
NSString *date2String = [formatter stringFromDate:date2];
//Comparison of Two dates by its conversion into string as below
if([date2String isEqualToString:dateString])
{
//Your logic if dates are Equal
}
else if(![date2String isEqualToString:dateString])
{
//Your Logic if dates are Different
}
EDIT:
Checkout this link.
Comparing dates
http://www.iphonedevsdk.com/forum/iphone-sdk-development/64625-how-compare-2-dates.html
Hope This Helps You. :)
Use NSCalendar and NSDateComponents to get a date components object. Then you can look at only those parts of the date that you care about.
If you're just trying to determine whether two dates are the same, regardless of time, one way to go is to use NSDate's -timeIntervalSinceDate: method. If the time interval returned is less than 86,400 seconds (i.e. 24 hours * 60 minutes * 60 seconds) then you can feel fairly sure that it's the same day. Changes related to such things as daylight savings time and leap seconds introduce some possibility of error... if that's a problem, go with NSDateComponents.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setLocale:[NSLocale autoupdatingCurrentLocale]];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];

How do I get the current date?

How can I get the current date, and format it like "20/12/2010"?
Use NSDate and NSDateFormatter.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
See also.
Break it down.
How do I get the current date? See NSDate.
How do I format a date in a specific format? See NSDateFormatter.
Here is a method to retrieve the current date and print as a string using Swift on IOS 9
let formatter : NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "d/M/yy"
let myStr : String = formatter.string(from: NSDate.init(timeIntervalSinceNow: 0) as Date)
print(myStr)
XCode Debug Print
5/6/16