0

I want to save the the current date and time in GMT in my core data. The field for current date in the table is set as Date field. I have no chance to change the field in to NSString.

NSDate *memberShipDate = [NSDate dateWithTimeIntervalSince1970:[intervalString doubleValue]];          
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[formatter setTimeZone:gmt];    
NSString *newmemberShipDate = [formatter stringFromDate:memberShipDate];

Here the newmemberShipDate is in NSString format. When I am trying to save this data to my db, it will become error. I need the newmemberShipDate in NSDate format.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • An NSDate object has no "format" (or timezone) -- it's a pure number. – Hot Licks May 06 '14 at 11:57
  • `stringFromDate`? With what `dateFormat`? from your `NSDateFormatter`? – Larme May 06 '14 at 11:59
  • what error message did you get? better if you could paste error log in your question. – Bharat May 06 '14 at 11:59
  • @bharath is it possible to save the nsstring in the field which have date type in the table.? – user3608008 May 06 '14 at 12:02
  • Edit your question and paste error log along with more code so one can help you. – Bharat May 06 '14 at 12:06
  • @bharath my question is this possible to get the newmemberShipDate in nsdate format? – user3608008 May 06 '14 at 12:08
  • Are you asking how to get todays date in a NSDate? `NSDate *today = [NSDate date];` Or you could assign your Core Data variable directly – Flexicoder May 06 '14 at 12:15
  • @Larme i think you understand my question.You edit and corrected my question.If you know the answer please help me.I am new in Objective c. – user3608008 May 06 '14 at 12:16
  • @Flexicoder correct but i need it in gmt format and the the final result not in the string format. – user3608008 May 06 '14 at 12:17
  • @user3608008: NSDate don't really care about time zones! – Larme May 06 '14 at 12:17
  • @Larme I think it will return local time.?In my case it is in IST. – user3608008 May 06 '14 at 12:19
  • I don't understand. If you use [NSDate date] for current date, no issue. – Larme May 06 '14 at 12:20
  • Yes you can convert `NSString` into `NSDate`. what date format have you applied to your dateformatter? – Bharat May 06 '14 at 12:20
  • @bharath NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [formatter setTimeZone:gmt]; – user3608008 May 06 '14 at 12:25
  • @bharath this is my error log..Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "memebershipStartDate"; desired type = NSDate; given type = __NSCFConstantString; value = .' – user3608008 May 06 '14 at 12:34
  • This is because you are trying to save a string value in date field. try to convert your string with appropriate date format.More more info check [apple guide](https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html) or [this thread](http://stackoverflow.com/questions/13506102/ios-date-format-20-sep-2012) or other similar post. – Bharat May 06 '14 at 13:03
  • Have you looked at this one http://stackoverflow.com/questions/1862905/nsdate-convert-date-to-gmt – Flexicoder May 06 '14 at 14:17
  • Let me repeat this: **An NSDate object contains no format or timezone information. It is purely a number.** You cannot format the NSDate object itself, all you can do is produce a string representation of the NSDate value which is formatted to your specifications. If your (unspecified) database accepts NSDate objects then you need do nothing. If not, you need to format the date into string format that your DB accepts. (And we have no way of knowing what that might be unless you tell us what DB you're using.) – Hot Licks May 06 '14 at 20:48

0 Answers0