1

I am setting NOW DATETIME like this

 NSDate* now = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone systemTimeZone] secondsFromGMT]];

And used value name 'now', trying convert to NSString like this

    NSDate *date = [object valueForKey:@"dateTime"];// valueForKey:@"dateTime"=now
    NSLog(@"%@",date);
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];


    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"yyyy"];
    NSString *year = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"MM"];
    NSString *month = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"dd"];
    NSString *day = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"HH"];
    NSString *hour = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"mm"];
    NSString *minute = [formatter stringFromDate:date];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"ss"];
    NSString *second = [formatter stringFromDate:date];

NSLog(@"%@",date);
NSLog(@"%@/%@/%@ %@:%@:%@ ",year,month,day,hour,minute,second);

consol LOG

2014-03-24 17:32:36.034 SecurityGate[7147:60b] 2014-03-24 15:51:29 +0000
2014-03-24 17:32:36.037 SecurityGate[7147:60b] 2014/03/25 00:51:29 

I want to make same(system time is Japan.tokyo) Please help me.

user3452170
  • 57
  • 2
  • 9
  • Are you aware of timezones? In your case the description of your date, which is used for NSLog will always print in GMT indicated by +0000 at the end of the output. – Volker Mar 24 '14 at 08:48
  • possible duplicate of [Why NSDate is reporting the wrong date?](http://stackoverflow.com/questions/6741519/why-nsdate-is-reporting-the-wrong-date) – Volker Mar 24 '14 at 08:48
  • 1
    possible duplicate of [Getting date from \[NSDate date\] off by a few hours](http://stackoverflow.com/questions/8466744/getting-date-from-nsdate-date-off-by-a-few-hours) – Martin R Mar 24 '14 at 08:49
  • PS: 17:32pm is no time to be trying to figure out date problems! Dates are the most complicated problem a programmer will ever face. Time to go home for the night and tackle it in the morning. ;-) – Abhi Beckert Mar 24 '14 at 09:32
  • NSDate* now = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone systemTimeZone] secondsFromGMT]]; is correct data what i want. And I using Core data (valueForKey:@"dateTime"=now means, valueForKey:@"dateTime" is core data value.) But when I trying convert to NSString, return different date value. 2014-03-24 15:51:29, 2014/03/25 00:51:29 is same time.(+9 hour problem) – user3452170 Mar 24 '14 at 10:07

1 Answers1

0

Your bug is in this line of code:

NSDate *date = [object valueForKey:@"dateTime"];// valueForKey:@"dateTime"=now

The comment claims it is "now" but it's not. Your NSLog output is:

2014-03-24 17:32:36.034 SecurityGate[7147:60b] 2014-03-24 15:51:29 +0000

Notice at the start it has the time of the NSLog, presumably in your local timezone (tokyo?).

But the date printed is totally different. The hour could be explained by timezone issues, but the minutes/seconds are also wrong. Converting it to your local timezone gives 2014-03-25 00:51:29, which is a full six hours in the future from the time I'm writing this post.

So clearly, the data inside the object is not "now" as you seem to think.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
  • sorry to have confused you. 'now' is not exactly 'NOW'. 'now' means, Data input time. (in different method, Always past) And I think i have +9 hour problem. (japan time between GMT). I want to fixed it. – user3452170 Mar 24 '14 at 10:15