I have two user selected dates: startDate and endDate. They are NSDate instances and I have to send them as parameters as NSNumber
s. How can I convert them to NSNumber with seconds?
Asked
Active
Viewed 398 times
2

AbdulAziz Rustam Ogli
- 139
- 12
-
You mean NStimeInterval? then from there you can format the date – Joshua Dec 22 '16 at 06:57
-
You shouldn't send raw date, because than you'll have problems with different time zones. – NSDmitry Dec 22 '16 at 10:16
3 Answers
3
Use below code :
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
// set format however you want
[formatter setDateFormat:@"ddMMyyyy"];
NSDate *date = [NSDate date];
NSString *string = [formatter stringFromDate:date];
NSNumber *num1 = @([string intValue]);
NSLog(@"%@",num1);

Himanth
- 2,381
- 3
- 28
- 41
-
I tried. It is taking the date as string in the form @"22/01/2016", but then when taken as int there is only 22 (int). – AbdulAziz Rustam Ogli Dec 22 '16 at 07:27
-
' NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; dateFormat =SHORT_DATE_FORMAT; [formatter setDateFormat:dateFormat]; self.stringfr = [formatter stringFromDate:date]; self.startDate = @([self.stringfr intValue]);' – AbdulAziz Rustam Ogli Dec 22 '16 at 07:40
-
-
-
-
-
-
2
1) First, get the date in MM/dd/yyyy
format:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
2) get string date and remove '/' from it:
NString *string = [formatter stringFromDate:date];
NString *finalStr = [string stringByReplacingOccurrencesOfString:@"/" withString:@""];
3) Use NSNumberFormatter
from converting NSString
to NSNumber
:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *myNumber = [f numberFromString: finalStr];
Hope, this is what you want!

sweta.me
- 273
- 1
- 11
0
If you mean a timestamp format NSDate *date = [NSDate date]; NSTimeInterval ti = [date timeIntervalSince1970]; How to convert NSDate into Unix timestamp in Objective C/iPhone?
Thanks,

Community
- 1
- 1

Nada Gamal
- 382
- 4
- 14