How to send NSDate with a certain date format as NSNumber? i.e convert NSDate to NSDate with another format?

420 views Asked by At

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

There are 3 answers

0
sweta.me On BEST ANSWER

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!

8
Himanth On

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);
0
Nada Gamal On

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,