I am getting the date from web service like /Date(1326067200000)/ , how can I convert it to the date like DD.MM.YYYY?
Use the Date() constructor to convert milliseconds to a date, e.g. const date = new Date(timestamp) . The Date() constructor takes an integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 UTC and returns a Date object.
dateFormat = @"MMMM dd, yyyy"; NSString* dateString = [formatter stringFromDate:date]; Convert a String to a Date: NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
To convert a second measurement to a millisecond measurement, multiply the time by the conversion ratio. The time in milliseconds is equal to the seconds multiplied by 1,000.
To convert milliseconds to seconds, divide the number of milliseconds by 1000 and then call the Date(timeIntervalSince1970:) with the resulting seconds. To avoid having to do this every time, you can write an extension to do it.
You can use
NSString *actDate = @"/Date(1326067200000)/";
NSString *nDate = [[[[actDate componentsSeparatedByString:@"("] objectAtIndex:1] componentsSeparatedByString:@")"] objectAtIndex:0];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:([nDate doubleValue] / 1000)];
In date
you will get the actual date. Further you can format it in "MM/dd/yyyy" format by using
NSDateFormatter *dtfrm = [[NSDateFormatter alloc] init];
[dtfrm setDateFormat:@"MM/dd/yyyy"];
nDate = [dtfrm stringFromDate:date];
In nDate
you will get your desired date in a formatted way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With