Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert system millisecond to the Date in objective-c

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

like image 364
OzBoz Avatar asked Jan 10 '12 09:01

OzBoz


People also ask

How do you convert milliseconds to Date format?

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.

How do I get the current Date in Objective C?

dateFormat = @"MMMM dd, yyyy"; NSString* dateString = [formatter stringFromDate:date]; Convert a String to a Date: NSDateFormatter* formatter = [[NSDateFormatter alloc]init];

How do you convert milliseconds to time?

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.

How do you convert milliseconds to seconds in Swift?

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.


1 Answers

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.

like image 190
Suvo08 K Avatar answered Oct 08 '22 06:10

Suvo08 K