2

I have two user selected dates: startDate and endDate. They are NSDate instances and I have to send them as parameters as NSNumbers. How can I convert them to NSNumber with seconds?

3 Answers3

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
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