mktime(from php) to iphone time(string) - iphone

I have in variable value from PHP function mktime(time from epoch), exacly 133123088.
How I can change it for readable date in iphone?
Is any function to convert this format to another like NSDate or just string ?

NSDate* date = [NSDate dateWithTimeIntervalSince1970: 133123088]

NSTimeInterval timeInterval = 133123088; // NSTimeInterval is a double
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
Notice that 133123088 corresponds to a date in 1974. So I guess you are missing a digit in 133123088 to make it a date in 2012: For example : 1331230880
NSTimeInterval timeInterval = 1331230880;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy/MM/dd HH:mm:ss";
NSString *dateString = [formatter stringFromDate:date];

Related

About JSON Date Conversion error

I get JSON Parse Date, I want to use NSDateFormatter,but not success.
1.JSON Get console Log
IncidentReceiveTime = "/Date(1353914100000+0800)/";
2.my code
NSString *dateStr =[NSString stringWithFormat:#"%#",[[[DateSortArry objectAtIndex:0] objectForKey:#"IncidentReceiveTime"]stringByReplacingOccurrencesOfString:#"/" withString:#""]];
NSLog(#"dateStr:%#",dateStr);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:#"yyyy-MM-ddHH"];
NSDate *date = [dateFormatter dateFromString:dateStr];
NSLog(#"date:%#",date);
3.NSDateFormatter console log
dateStr:Date(1361943694000+0800)
date:(null)
Refer to my answer in :
iPhone:How to convert Regular Expression into Date Format?
Here I am posting this code from my answer in the above link as if in case in future the above link goes down then this answer doesn't become useles:
- (NSDate*) getDateFromJSON:(NSString *)dateString
{
// Expect date in this format "/Date(1268123281843)/"
int startPos = [dateString rangeOfString:#"("].location+1;
int endPos = [dateString rangeOfString:#")"].location;
NSRange range = NSMakeRange(startPos,endPos-startPos);
unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];
NSLog(#"%llu",milliseconds);
NSTimeInterval interval = milliseconds/1000;
return [NSDate dateWithTimeIntervalSince1970:interval];
}
Once you get NSDate object from above method:
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString *dateStr = [dateForm stringFromDate:<YourNSDateObject>];
[dateForm setDateFormat:#"<Your Desired Date Format>"];
NSDate *desireddate = [dateForm dateFromString:dateStr];
Hope this helps.

Parse Date to timestamp

I have such response from server Date = "/Date(1348783200000+0200)/" how can I parse it to timestamp or date (example: Monday 21, September, 2012)?? please help..
It looks like your date is in milliseconds. You will need to divide by 1000 and cast it into an NSDate after that. Then you can just use NSDateFormatter with NSDateFormatterLongStyle to show the date in that format.
NSTimeInterval dateInterval = dateWithMilliseconds;
dateInterval = dateInterval / 1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateInterval];
NSDateFormatter *formatter = [NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSString *dateString = [formatter stringFromDate:date];

NSDate and NSDateFormatting and unix timestamp

So I have the following code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:self.firstUsed];
NSLog(#"FIRST USED %# %f", dateString, [[NSDate date] timeIntervalSinceDate:self.firstUsed]);
FIRST USED 06/15/2012 10:42PM 716.087895
The thing that confuse me is that when I put in 173.755023 it doesn't translate back to June 15th 2012 10:42. How is this possible? I think the correct number should be 1339714440
Try this :
NSDate* referenceDate = [NSDate dateWithTimeIntervalSince1970: 0];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
int offset = [timeZone secondsFromGMTForDate: referenceDate];
int unix_timestamp = [curdate timeIntervalSince1970];
int Timestamp = unix_timestamp - offset;
Please write down Following Code :
NSDateFormatter *dtFormatAppointment = [[NSDateFormatter alloc] init];
[dtFormatAppointment setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSString *dateString = [dateFormat stringFromDate:self.firstUsed];
NSTimeInterval time = [dateString timeIntervalSince1970];
NSLog(#"FIRST USED %# %.f", dateString, time);
this code is working for you.

Parsing custom string gives wrong date

I have a string that has a date format of HH:mm so for example it could 12:00 or 22:00, and I input that into my NSDateFormatter by setting it as the date format. I just need to construct a custom date. The problem is when I have done this and I get my parsed string as 2012-05-17 12:00:00 +0000 if the date is the 17th of May.
NSDate *today = [NSDate date];
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateFormat:[NSString stringWithFormat:#"yyyy-MM-d %#:00 +0000",#"12:00"]];
NSString *finalTodayString = [output stringFromDate:today];
parsedDateString = [NSString stringWithString:finalTodayString];
The problem is when I parse it again to just include the HH:mm I get something totally different. For example if I have this code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-d HH:mm ZZZ"];
NSDate *date = [dateFormatter dateFromString:fullDateString];
NSDateFormatter *dateFormmater2 = [[NSDateFormatter alloc] init];
[dateFormmater2 setDateFormat:#"HH:mm"];
NSString *string =[dateFormmater2 stringFromDate:date];
Then the string should be 12:00 but instead it becomes 14:00. Please help.
Remove the Greenwich Meridian time "+0000" from the first code

How do I get the current date?

How can I get the current date, and format it like "20/12/2010"?
Use NSDate and NSDateFormatter.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
See also.
Break it down.
How do I get the current date? See NSDate.
How do I format a date in a specific format? See NSDateFormatter.
Here is a method to retrieve the current date and print as a string using Swift on IOS 9
let formatter : NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "d/M/yy"
let myStr : String = formatter.string(from: NSDate.init(timeIntervalSinceNow: 0) as Date)
print(myStr)
XCode Debug Print
5/6/16