0

hi am using following to convert date to string in objective C.

-(void)convertDateToJsonString:(NSString *)strDate
{


    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setDateFormat:@"yyyy/MM/dd"];


    NSDate * date =[formatter dateFromString:strDate];

    long long milliseconds = (long long)([date timeIntervalSince1970] * 1000.0);

    NSLog(@"After conversion %lli",milliseconds);



}

its giving date but always getting time is 00:00:00 something not current time.How to get selected date with current time in milli second.

but its working for current date like below

 long long millisecs = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);

    NSLog(@"After conversion %lli",millisecs);

Am getting from date picker date and converting.

And also for getting exact time zone like +530 in ios is there any methods? i know [NSTimezone Systemzone] but its giving whole value like "Asia/kolkatta +..."

Sugan S
  • 1,782
  • 7
  • 25
  • 47
  • 1
    if the initial string date doesn't have any hours or minutes. How could `dateFromString` guess? – Larme Dec 09 '13 at 15:14
  • so i have to get time also from string then can get milli second right? @Larme – Sugan S Dec 09 '13 at 15:21
  • You don't show us how you display time, so anything we say would be a conjecture, other than to observe that if you only parse date and not time then you won't magically get time from somewhere. – Hot Licks Dec 09 '13 at 17:00
  • "Am getting from date picker date and converting." If you're getting the source date from a date picker, why don't you just leave it as an NSDate, vs converting to string and back? – Hot Licks Dec 09 '13 at 17:04
  • To get the timezone in the format you desire simply use "Z" in the date format string of the date formatter you use to convert the NSDate to a string date. – Hot Licks Dec 09 '13 at 17:05

2 Answers2

0

The problem is that your strDate is not getting parsed by dateFromString:. The result is that your date will be nil and timeInternvalSince1970 will do some default processing on nil. You can confirm this by adding:

NSAssert (nil != date);

after your initialize date. The assertion will fail.

The solution is that you need a better formatter - one that is consistent with the format of the passed in strDate.

But, perhaps you have a more fundamental problem. The UIDatePicker date property returns an NSDate object directly. Where are you getting your strDate from?

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • hi @GoZoner am getting date is correct only the problem is time always 00:00:00+5:30 am getting.Thanks for your reply friend – Sugan S Dec 09 '13 at 16:51
  • Okay. Why are you using a `strDate` as an `NSString` instead of just getting an `NSDate` directly from your `NSDatePicker` using the `date` property? – GoZoner Dec 09 '13 at 19:30
0

You have not added hours, minutes and seconds in NSDateFormatter like this

[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss ZZZ"];

sanjaymathad
  • 232
  • 1
  • 5