How to Convert a particular NSDate to UNIX time stamp in ios - iphone

I want to convert my date Object to Unix time stamp.
// "**strSellTime**" has the string of my date
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"EEEE MMM yyyy-MM-dd hh:mm:ss a"];
NSDate *date=[dateFormatter dateFromString:strSellTime];
and wants to convert this date to UNIX time stamp like (1395382740) in this format.

Just use
[(NSDate*)date timeIntervalSince1970]

time_t unixTime = (time_t) [date timeIntervalSince1970];
was able to solve it by this

Related

Converting the time in datetime format in iPhone

To all:
Can someone please help me about how to convert the time (HH:mm) format to the datetime(yyyy/mm/dd HH:mm:ss) format? I need help.
I already tried this String to a Format yyyy-MM-dd HH:mm:ss Iphone link, but I am having the string in this format HH:mm and I want to convert this into the yyyy/mm/dd HH:mm:ss
Thanks in advance!!
#Neelz saying right If in DB you are saving only hours & minute value then it will be depends on you which date you are using to get the formatted date.I did the same which #Neelz said but in different way.Below I shown the code by considering the today's date to get the date time as you need
//get the today's date
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:#"en_US"]];
//Set the required date format to get the todays date
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString* cCurrentDateStamp = [formatter stringFromDate:date];
NSLog(#"%#",cCurrentDateStamp);
//now get the hours & minute you saved in DB.I considered 11:08
NSString *strYOurTime=#"11:08";
//create the date time string by appending date & time
NSString *str=[NSString stringWithFormat:#"%# %#:00",cCurrentDateStamp,strYOurTime];
//set the formatter
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *FinalDateTime=[formatter dateFromString:str];
NSLog(#"%#",FinalDateTime);
FinalDateTime is the final date which you want.Now to get the number of days between two dates refer following link:
iPhone - get number of days between two dates

Required NSDate in PST

I want current date and time in PST. I used this code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"String:%#",timeStamp);
It returns correct date and time in PST in string form but I want NSDate in PST. So when I change NSString to NSDate like this:
NSDate *currentPST = [dateFormatter dateFromString:timeStamp];
NSLog(#"currentPST Date:%#",currentPST);
It returns date in GMT. I have done R&D but all in vain.Output is:
String:2011-05-18 22:28:54 PDT
currentPST Date:2011-05-19 05:28:54 +0000
Can anyone suggest a solution please.
Thanks in advance
In Cocoa, NSDate is an abstract representation of a date with no time zone information applied.
Whenever you print a NSDate object, it will print the date value corresponds to the default timezone(your device timezone). Your device timezone is GMT thats why you get the value like that. If you look into that deeply, both the time where same, but the timezone varies.

Wrong time from NSDateFormatter

I have a string that I want to parse the time from:
NSString *longdate = #"Mar 27, 2011 8:38:38 PM";
I want to parse this date and output just the time portion w/ hours+minutes+am/pm:
// First, convert our string into an NSDate
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"MMM dd, yyyy HH:mm:ss aaa"];
NSDate *date = [inFormat dateFromString:longdate];
[inFormat release];
// Now convert from date back to a string
NSDateFormatter *outFormat = [[NSDateFormatter alloc] init];
[outFormat setDateFormat:#"HH:mm aaa"];
NSString *final = [outFormat stringFromDate:date];
[outFormat release];
NSLog(#"original: %# | final %#", longdate, final);
The problem is the final time is wrong. I expect the time to be 8:38 PM, but instead I get 12:38 PM.
I just want to get the same hour out that I put it, and not bother w/ any time zones or locales. What am I doing wrong here? Thanks.
Found the problem. Had nothing to do with timezones and everything to do with using the wrong formatting codes for the date formatter.
[inFormat setDateFormat:#"MMM dd, yyyy HH:mm:ss aaa"];
should be:
[inFormat setDateFormat:#"MMM dd, yyyy h:mm:ss aaa"];
Likewise, outFormat's dateformat should be:
[outFormat setDateFormat:#"h:mm aaa"];
After this adjustment everything works fine even w/o any TimeZone adjustments.
As Dave said, check your time zones. You can tell the date formatter to use your current time zone as well:
[outFormat setTimeZone:[NSTimeZone localTimeZone]];
You've got your timezones messed up. IIRC, NSDateFormatter (by default) will parse stuff in the UTC timezone (+000), but dates are NSLogged in your current timezone. Or something like that.
Anyway, check your timezones.

NSDateFormatter, NSDate, UTC and strange date format

I have a very strange date format coming to me via JSON. e.g. - "July, 18 2010 02:22:09"
These dates are always UTC. I'm parsing the date with NSDateFormatter, and setting the timeZone to UTC...
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[inputFormatter setDateFormat:#"MMMM, dd yyyy HH:mm:ss"];
NSDate *formatterDate = [inputFormatter dateFromString:dtStr];
However, the date when logged is appearing with the offset of my device...
"2010-07-18 02:22:09 -0600"
What am I doing wrong here?
I think this is because your NSDate isn't using the UTC timezone. The docs say this about the description method of NSDate:
A string representation of the receiver in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”).
So, you aren't really doing anything wrong. NSDate is behaving as expected. What are you trying to accomplish? Do you need the date in a string? Are you doing date comparison?

iPhone: convert Nsstring to Nsdate on device timezone

I received date from Web service which is in GMT + 1 format (2010-02-25T20:16:50.387+05:30).
I want to convert in NSdate. I have no proper idea about date formatter.
Please suggest that how can i Change this nsstring value to current date format (Time zone)
Convert "2010-02-25T20:16" into NSDate using DateFormatter:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyyMMdd'T'HHmm";
NSDate *gmtDate = [formatter dateFromString:#"2010-02-25T20:16"];
Once you have the GMT date, you can add/substract the GMT-your time zone offset to it to get the current NSDate in your time zone. To get the offset between your timezone and GMT, you can use the following:
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSDateFormatter is all about converting dates to and from string representations. Take a look at the docs for that.