I have this "1273636800000" and I want to convert it to this format "Wed Mar 03 2010 00:00:00 GMT-0500 (EST)"
I need to convert this milliseconds to NSDate format.
I tried this
NSDate *tr = [NSDate dateWithTimeIntervalSince1970:1273636800000];
and
NSDate *tr = [NSDate dateWithTimeIntervalSinceReferenceDate:1273636800000];
and
NSDate *tr = [NSDate dateWithTimeIntervalSinceNow:1273636800000];
But I can't get it to work, anyone had a similar problem and found the solution
These functions (dateWithTimeIntervalSince1970, dateWithTimeIntervalSinceReferenceDate, dateWithTimeIntervalSinceNow) accept parameter in seconds.
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(ms / 1000.0)];
You should pass 1273636800.000
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(1273636800 / 1000.0)];
Divide by 1000 to get millis in seconds, that's what you want.
A simple one-line Swift 2 function:
func dateFromMilliseconds(ms: NSNumber) -> NSDate {
return NSDate(timeIntervalSince1970:Double(ms) / 1000.0)
}
Related
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?
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.
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];
use this:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
then convert your date in the format you want.
I have the date string "2016-04-01T05:00:00+08:00" and I want to convert it into number of seconds since 1 January 1970. I am doing like this:
NSDateFormatter *parsingFormatter = [NSDateFormatter new];
[parsingFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [parsingFormatter dateFromString:"2016-04-01T05:00:00+08:00"];
NSTimeInterval startTime = [date timeIntervalSince1970];
But in the above example startTime returns nil. Can someone tell me what I am doing wrong? How can I convert it?
Try this
NSDateFormatter *parsingFormatter = [NSDateFormatter new];
[parsingFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [parsingFormatter dateFromString:#"2016-04-01T05:00:00+08:00"];
NSTimeInterval startTime = [date timeIntervalSince1970];
I am getting date "2016-03-31 21:00:00 +0000" UTC format.
I am trying to convert a date to milliseconds then from milliseconds to date in objective c. But variance occurs. I had used the following code.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = #"MMM dd, yyyy HH:mm:ss a";
NSDate *dateHolder = [dateFormat dateFromString: #"Nov 25, 2014 4:11:50 PM"];
NSTimeInterval seconds = [dateHolder timeIntervalSince1970];
double milliseconds = seconds*1000;
NSLog(#"Date milliseconds %f", milliseconds);
NSDate *tr = [NSDate dateWithTimeIntervalSince1970: (milliseconds/1000)];
NSLog(#"Date by milliseconds%#", tr);
Output is received as follows
Date milliseconds 1416897710000.000000
Date by milliseconds 2014-11-25 06:41:50 +0000 // Hours and minutes were varied
How to fix ? Thanks in advance, for any help.
My output
Date milliseconds 1416913910000.000000
Date by milliseconds2014-11-25 11:11:50 +0000
Maybe because of the Time Difference ?
This question already has an answer here:
NSDate from stange looking string
(1 answer)
Closed 9 years ago.
I get the birthday by parsing the xml. In
-(void)parser:(NSXMLParser*)parser didEndElement
method ,I convert the value of the <birthday></birthday> element into NSDate but it returns /Date(934146000000+0300)/:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:currentStringValue];
How can I convert it into NSDate to be able to converting into NSString?
It looks like you're retrieving the value from a long var.
It currently refers to Mon, 29 Nov 31571 00:00:00 GMT
If you remove three 0s from the end it makes sense...
934146000 = Sun, 08 Aug 1999 21:00:00 GMT
You also need to fix the formatting. The format dd-MM-yyyy will not be able to understand 934146000.
You can do something like...
CGFloat dateValue = 934146000;
NSDate *theDate = [NSDate dateWithTimeIntervalSince1970:dateValue];
This should get the correct date.
But like I said, you need to sort out the value you are getting first.
Seems you are getting the value from Java or C#, as current time in millisecond.
In Objective C, NSDate expect the time as second, not in millisecond. So you have to divide it by 1000.
NSDate * dateOfBirth = [NSDate dateWithTimeIntervalSince1970:[recvDate floatValue]/1000.0];
You can use something like below, but you still need correction for the millisecond to second value.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"EEE, dd MMM yyyy HH:mm:ss ZZZ";
NSDate * date = [formatter dateFromString:str];
I want to convert my timestamp value 1308031456 to NSDate format (which yields the value Tue, 14 Jun 2011 06:04:16 GMT in online web conversion). How would I convert between the two programmatically?
OBJ - C:
Use dateWithTimeIntervalSince1970:,
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
SWIFT:
Use init(timeIntervalSince1970:)
let date = NSDate(timeIntervalSince1970:timeStamp)
You can use the use the method dateWithTimeIntervalSince1970: to convert the timestamp you've which is the epoch time.
NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:1308031456];
In obj c
double timeStamp = 1513330403393;
NSTimeInterval unixTimeStamp = timeStamp / 1000.0;
NSDate *exactDate = [NSDate dateWithTimeIntervalSince1970:unixTimeStamp];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yyy hh:mm a";
NSString *finalate = [dateFormatter stringFromDate:exactDate];
In Swift
let timeStamp = 1513330403393
let unixTimeStamp: Double = Double(timeStamp) / 1000.0
let exactDate = NSDate.init(timeIntervalSince1970: unixTimeStamp)
let dateFormatt = DateFormatter();
dateFormatt.dateFormat = "dd/MM/yyy hh:mm a"
print(dateFormatt.string(from: exactDate as Date))
See the various example on date formatting and getting date from timestamp from apple
Use Formatter Styles to Present Dates and Times With the User’s Preferences
Convert TimeStamp to NSDate in Objective-C
Here is a code piece which lets you convert the unix type timestamp to Formatted Date
NSString * timeStampString =#"1304245000";
NSTimeInterval _interval=[timeStampString doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:#"dd.MM.yyyy"];
NSString *_date=[_formatter stringFromDate:date];
This will print _date something like 05.03.2011