Reputation: 2593
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?
Upvotes: 1
Views: 6613
Reputation: 2786
use this:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
then convert your date in the format you want.
Upvotes: 0
Reputation: 1181
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.
Upvotes: 10
Reputation: 303
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];
Upvotes: 2