This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Create NSDate from Unix timestamp
I have an application where i am receiving date from json in unix timestamp format.This is the timestamp that i am receiving from json '1357178589630'.How to convert this timestamp into correct nsdate.I have used the following code for conversion but it is not working properly.This is my code:
double timestampval = [[updates objectForKey:#"timestamp"] doubleValue];
NSTimeInterval timestamp = (NSTimeInterval)timestampval;
NSDate *updatetimestamp = [NSDate dateWithTimeIntervalSince1970:timestamp];
When the timestamp is converted to nsdate using datewithtimeIntervalSince1970,in the updatetimestamp variable it displays '44977-04-11 12:40:30 +0000'
Try this one:
Might be you are getting timestamp is milli seconds instead of seconds, so you divide it by 1000.
EDIT Newer:
double timestampval = [[updates objectForKey:#"timestamp"] doubleValue]/1000;
NSTimeInterval timestamp = (NSTimeInterval)timestampval;
NSDate *updatetimestamp = [NSDate dateWithTimeIntervalSince1970:timestamp];
Previous :
double unixTimeStamp =1304245000;
NSTimeInterval timeInterval=unixTimeStamp/1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setLocale:[NSLocale currentLocale]];
[dateformatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString=[dateformatter stringFromDate:date];
[NSDate dateWithTimeIntervalSince1970:timestamp] is correct. Seems that your timestamps is wrong.
Instead 1357178589630 it should be 1357178589.
Try here.
Update: as per Martin comment above.
Just divide the value by 1000.
try this code..
NSDate *dateTraded = [NSDate dateWithTimeIntervalSince1970 :[[updates objectForKey:#"timestamp"] integerValue]];
and Unix timestamps are in seconds, the value you have looks like a number of milliseconds since 1st January 1970. If you divide by 1000, you get 1264396813, which according to http://www.onlineconversion.com/unix_time.htm
Related
This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
When i create an object for NSDate and put cursor on that object it was showing currect date like below
and when am trying to log that object it was showing date with minus 5:30hr like below
and when I add offset to date and put cursor on that object it was showing date with plus 5:30hr like below
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
and when am trying to log that object it was showing exact date like below
Why its happening like this? Is this problem with timezone?
Thanks in advance.
NSDate always comes as a GMT. So you have to format the date as per your required timeZone.. You can set your device timeZOne or can use Calendar to set the timeZOne..
NSDate is a "raw" date. That's why it is in GMT and your local and default time zone is GMT +5. It's up to the code to use NSDateFormatter to output the date to a value that makes sense for the user. Try this.
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate]];
NSDate description return string representation in the international
format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone
offset in hours and minutes from GMT.
If you want currentLocale, Use:
[date descriptionWithLocale:[NSLocale currentLocale]]
This question already has an answer here:
NSDateFormatter not giving me correct
(1 answer)
Closed 9 years ago.
this is the code that i am using for changing date formate
NSLog(#"newBirthDates%#",_newwBirthDates);
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setDateFormat:#"MM/dd"];
NSDate *date1 =[NSDate date];
NSString *string =[Form stringFromDate:date1];
NSLog(#"string%#",string);
NSDate *todaydate =[Form dateFromString:string];
NSLog(#"todaydate%#",todaydate);
this is what i get as output
newBirthDates(
"05/22",
"07/11",
"10/07",
"02/20"
)
newBirthDates(
"05/22",
"07/11",
"10/07",
"02/20"
)
string03/18
todaydate1970-03-17 18:30:00 +0000
now my question is why 3/18 become 03/17?? why one day get decreases
The answer is simple - time zones.
Take a close look at what NSLog prints out
1970-03-17 18:30:00 +0000
By default, a NSDateFormatter is set to your local timezone. That means, if your time zone is +5:30 giving it a date "1970/18/3" results in 1970-03-18 00:00:00 +0530.
However, NSLog always prints dates in GMT (zero) time zone, adding/substracting the time zone difference (5 hours and 30 minutes).
Basically, there is nothing to fix, you just have to understand how NSLog works if you want to use it to check NSDate values.
Your Log is showing as per string value, eliminating all important timezone differences.
Log of NSDate shows you the time from GMT.
And both the values are correct.
The sole primitive method of NSDate, timeIntervalSinceReferenceDate,
provides the basis for all the other methods in the NSDate interface.
This method returns a time value relative to an absolute reference
date—the first instant of 1 January 2001, GMT.
You must read NSDate Documentation.
for getting correct date you can use this one,
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setDateFormat:#"MM/dd"];
[Form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date1 =[NSDate date];
NSString *string =[Form stringFromDate:date1];
NSLog(#"string%#",string);
NSDate *todaydate =[Form dateFromString:string];
NSLog(#"todaydate%#",todaydate);
above code will give the correct date.
The Main thing is TimeZone : [Form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
This is the Correct Code :
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[Form setDateFormat:#"MM/dd"];
NSDate *date =[NSDate date];
NSString *string =[Form stringFromDate:date];
NSDate *todaydate = [Form dateFromString:string];
NSLog(#"todaydate %#",todaydate);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
hours minutes seconds to seconds ios
I have a time formatted as such
// format: HH:mm:ss,AAA
// example for 2 hours, 35 minutes, 15 seconds, and 207 milliseconds
02:35:15,207
I'm trying to convert that into seconds as a double. The above example would turn into:
// 2 hrs * 3600 + 25 min * 60 + 15.207
9315.207
I figure I can pick apart each element with a scanner, but I'm thinking there's probably an easier way. I tried using NSDateFormatter but I need this as a double, not as an NSDate. Any suggestions? Thanks.
Further FYI
This is for use with the MPMoviePlayer and the currentTime property is a double. For any given section where I have data to show, I am checking if dataStartTime <= playerTime < dataEndTime. So I'm using double because that's the type for currentTime already.
Here is the solution, the idea is simple get two date one with your time and one with 00:00:00,000 time then take difference of their time.
- (double)secondsFromString:(NSString*)str {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss,SSS"];
NSString *dateString = #"1970-01-01";
NSDate *date = [formatter dateFromString:[NSString stringWithFormat:#"%# %#",dateString,str]];
NSDate *refDate = [formatter dateFromString:[NSString stringWithFormat:#"%# 00:00:00,000",dateString]];
double time = [date timeIntervalSince1970] - [refDate timeIntervalSince1970];
return time;
}
I have tried so many things through the help I got, but I still can't figure out how to do it properly. Here's what I did lastly.
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"dd-MM-yyy"];
NSDate *currentDate = [NSDate date];
NSDate *fromDate = [NSString stringWithFormat:#"%#",[tempFormatter stringFromDate:currentDate]];
NSLog(#"currentDate %#", fromDate);
NSDate *toDate = [NSString stringWithFormat:#"%#",[tempFormatter stringFromDate:datePicker.date]];
NSLog(#"toDate %#", toDate);
NSTimeInterval interval = [toDate timeIntervalSinceDate:fromDate];
double leftDays = interval/86400;
NSLog(#"Total interval Between::%g",leftDays);
Tell me what I did wrong. Is it the NSDate conversion, that I am not doing properly ??
Thanks.
Your code is all messed up -- both toDate and fromDate are strings not NSDates. Your from date should just be currentDate, and your toDate should just be datePicker.date. You don't need to do anything with converting to strings or using a date formatter to get the time interval.
This line is creating problem.
NSDate *toDate = [NSString stringWithFormat:#"%#",[tempFormatter stringFromDate:datePicker.date]];
It changes the type of toDate from NSDate to __NSCFString. The NSTimeInterval take both of its arguments of NSDate type, but in your case only fromDate is NSDate type.
Change your code with these lines
NSDate *currentDate = [NSDate date];
NSDate *toDate = datePicker.date;
NSTimeInterval interval = [toDate timeIntervalSinceDate:currentDate];
It will surely work (inshaAllah).
You're certainly on the right track; however, you seem to be calling "timeIntervalSinceDate" using two NSString's (even though you're specifying fromDate and toDate as NSDates, look right after that- you're setting those two variables to NSString objects).
To get the interval you're looking for, try:
[datePicker.date timeIntervalSinceDate:currentDate];
That should get you the right interval. In addition, you may want to change leftDays to equal
double leftDays = abs(round(interval/86400));
This will stop leftDays from being an awkward number like -1.00005.
`Passing NSString to NSDate! this code is wrong
try
NSDate *curDate = [NSDate Date];
NSDate *pickerDate = datepicker.date;
then compare both these dates using NSTimeInterval
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to Get time difference in iPhone
I´m getting date and time from a JSON feed. I need to find the difference between the date I´m getting from the feed and today´s date and time. Any suggestions how I can do this?
I know I need to subtract the current date with the date I get from the feed, but I don´t know how to do it.
Ex:
Date from feed: Date: 2011-06-10 15:00:00 +0000
Today: Date: 2011-06-10 14:50:00 +0000
I need to display that the difference is ten minutes.
Thanks!
Create two NSDate objects from the strings using NSDate's -dateWithString:, then get the difference of the two NSdate objects using
NSTimeInterval diff = [date2 timeIntervalSinceDate:date1];
You need to convert the input date to an NSDate object before you try and compare.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss +0000"];
NSDate *startDate = [dateFormatter dateFromString:yourJSONDateString];
NSDate *endDate = [NSDate date];
CGFloat minuteDifference = [endDate timeIntervalSinceDate:startDate] / 60.0;
The formatter assumses the UTC offset will always be zero. If this isn't true, see Microsoft's date format string page for other format codes you can use.
--
Edit: the dateWithString method that everyone else used will be better to use in your situation, but the date formatter is necessary if the date format string you are getting isn't exactly right. I don't think I've ever used an API that sent dates in the correct format, perhaps I'm just unlucky :-(.
From below code you will get an idea for comparing two NSDate objects.
NSDate *dateOne = [NSDate dateWithString:#"2011-06-10 15:00:00 +0000"];
NSDate *dateTwo = [NSDate dateWithString:#"2011-06-10 14:50:00 +0000"];
switch ([dateOne compare:dateTwo])
{
case NSOrderedAscending:
NSLog(#”NSOrderedAscending”);
break;
case NSOrderedSame:
NSLog(#”NSOrderedSame”);
break;
case NSOrderedDescending:
NSLog(#”NSOrderedDescending”);
break;
}