1

I have a date in timestamp which looks something like this: 1474914600000

Now, I want to covert this timestamp to NSDate in format of dd-mm-yyyy.

How can this be done in objective c?

Milan Gupta
  • 1,181
  • 8
  • 21
Madhu
  • 2,565
  • 2
  • 25
  • 32
  • 1
    Convert the timestamp into `NSDate`, and then use a `NSDateFormatter`. – Larme Oct 17 '16 at 10:18
  • Follow this link: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html , http://soulwithmobiletechnology.blogspot.com/2011/06/convert-timestamp-to-nsdate-in.html – Jamshed Alam Oct 17 '16 at 12:20
  • Possible duplicate of [Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?](http://stackoverflow.com/questions/2201216/is-there-a-simple-way-of-converting-an-iso8601-timestamp-to-a-formatted-nsdate) – Abha Oct 18 '16 at 09:07

3 Answers3

10

You need to convert your timestamp to NSDate and then get NSDate in your desired format. Also your timestamp seems to be in millisecond so you will have to divide it be 1000. You can use below code:

 double timeStamp = 1474914600000;
 NSTimeInterval timeInterval=timeStamp/1000;
 NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
 NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
 [dateformatter setDateFormat:@"dd-MM-yyyy"];
 NSString *dateString=[dateformatter stringFromDate:date];

dateString value as an output will be: "27-09-2016"

Hope it helps.

Milan Gupta
  • 1,181
  • 8
  • 21
2

To elaborate on balkaran's answer incase you're new to the iOS world. The timestamp you provided seems to go down to milliseconds which you wouldn't need for day times that's why he's dividing by 1000. You would use the dateformatter as follows to return an NSString you can use with the formatted date.

NSDate *date = [NSDate dateWithTimeIntervalSince1970:1474914600000];
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @"dd-MM-yyyy";
NSString *formattedDate = [formatter stringFromDate:date];
HarmVanRisk
  • 203
  • 1
  • 8
0

use this:

NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];

then convert your date in the format you want.

Milan Gupta
  • 1,181
  • 8
  • 21
balkaran singh
  • 2,754
  • 1
  • 17
  • 32