How to convert millisecond system to Date in objective-c

I get a date from a web service like / Date (1326067200000) /, how can I convert it to a date like DD.MM.YYYY?

+6
source share
4 answers

you can use

NSString *actDate = @"/Date(1326067200000)/"; NSString *nDate = [[[[actDate componentsSeparatedByString:@"("] objectAtIndex:1] componentsSeparatedByString:@")"] objectAtIndex:0]; NSDate *date = [NSDate dateWithTimeIntervalSince1970:([nDate doubleValue] / 1000)]; 

On date you will get the actual date. Then you can format it in the format "MM / dd / yyyy" using

  NSDateFormatter *dtfrm = [[NSDateFormatter alloc] init]; [dtfrm setDateFormat:@"MM/dd/yyyy"]; nDate = [dtfrm stringFromDate:date]; 

In nDate you will get the desired date in a formatted way.

+16
source

Use NSDate initWithTimeIntervalSinceReferenceDate: (assuming your value uses the first moment from January 1, 2001, GMT as a key date) to get an NSDate instance representing your timestamp.

To get a formatted string representation, you can use the NSDateFormatter class.

0
source

Try it...

 NSDate *date=[[NSDate alloc] initWithTimeIntervalSinceReferenceDate:gettingdate]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; [dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"]; NSString *newdate = [dateFormat stringFromDate:date]; 
0
source
 //Use of Function textFiled.text = [self displayDate:[[NSString stringWithFormat:@"%@",[[[AllKeys valueForKey:@"Date"] componentsSeparatedByString:@"\"]]]] // Millisecond to Date conversion - (NSDate *)displayDate:(double)unixMilliseconds { NSDate *date = [NSDate dateWithTimeIntervalSince1970:unixMilliseconds / 1000.]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterShortStyle]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; [dateFormatter setLocale:[NSLocale currentLocale]]; NSLog(@"The date is %@", [dateFormatter stringFromDate:date]); // NSDate *returnValue = [dateFormatter stringFromDate:date]; return date; } 
0
source

Source: https://habr.com/ru/post/905598/


All Articles