Thirteen figures converted into NSDate - ios

Code i have try
NSString *time = #"1470067200000";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)([time doubleValue])/1000];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"%#",dateString);
Result : 2016-08-02 00:00
The HH:mm is always wrong, why?

Set Time Zone in the NSDateFormatter
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)([time doubleValue])/1000];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"%#",dateString);

NSDate always be in UTC and if you apply DateFormatter then default timezone in DateFormatter is localTimeZone. When I run the below code its giving me the below dates. Please check your device/simulator timezone
NSString *time = #"1470067200000";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm"];
// [formatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)([time doubleValue])/1000];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"%#",dateString);
Printing description of date:
2016-08-01 16:00:00 +0000
Printing description of dateString: // after From NSDateFormatter
2016-08-01 21:30
To get the stringFromDate in UTC from NSDateFormatter use below line
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];

Related

How to convert UTC date string to local time

I currently get time as UTC and I want it to be convert into local time zone
NSDate * startDate;
Example strDate - 14/9/2017 7.28 Am
Required format - 14/9/2017 1.52 pm
I need help with objective c.
NSDate * startDate ; //contain utc date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
but this is not correct time but date is correct
First convert NSString to NSDate.
NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZoneEDT =[NSTimeZone timeZoneWithName:#"GMT"];
[DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[DateFormatter setTimeZone:timeZoneEDT];
[DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *CurrentDate = [DateFormatter dateFromString:DateStr];
Then convert GMT time to local time.
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
#athira try this. it works for me.
NSDate *startDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/M/yyyy h.mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
NSLog(#"date = %#",timestamp); //date = 14/9/2017 4.34 PM
it will print current time with proper GMT+5:30 and i have used localTimeZone.

Unable to get date from string?

I Get DateTime string from server "8/27/2016 6:56:23 AM"
And I want to fetch only date from this string.
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date1=[dateFormatter dateFromString:StreamStartDateTime];
But the problem is I am getting null in date1.
please help me to find out where I am doing wrong.
You are using the wrong dateFormat. Please replace your code with following:
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date1=[dateFormatter dateFromString:StreamStartDateTime];
do like
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy h:mm:ss a"]; or use hh:mm:ss a
NSDate *date1=[dateFormatter dateFromString:StreamStartDateTime];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSString *ssecond=[dateFormatter stringFromDate:date1];
NSDate *finalDate=[dateFormatter dateFromString:ssecond];
final output as
Your date format must be match with your date string.
So If your date string is 8/27/2016 6:56:23 AM then your format must be MM/dd/yyyy hh:mm:ss a. Then with this format get date from string first like,
NSDate *date=[df dateFromString:StreamStartDateTime];
Now, change date formatter in whatever format you want your date say,
[df setDateFormat:#"yyyyMMdd"];
then
NSString *resultDateStr = [df stringFromDate:date];
Can try like this
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *streamStartDate = [formatter dateFromString:StreamStartDateTime];
[formatter setDateFormat:#"yyyyMMdd"];
NSString *strDate = [formatter stringFromDate:streamStartDate];
NSDate *compDate = [formatter dateFromString:strDate]; // Get date in UTC format
Output:
Printing description of strDate:
20160827
Printing description of compDate:
2016-08-26 18:30:00 +0000
NSString *LiveStreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *date1=[dateFormatter dateFromString:LiveStreamStartDateTime];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *fisrtdate=[dateFormatter stringFromDate:date1];
NSDate *date11 = [dateFormatter dateFromString:fisrtdate];

Removing and reformatting of NSDate is not working?

I am trying to reformat this NSdate (2016-02-11 01:00:00 +0000 ) into this (11-02-2016).
I have used below code and its returning null.
//newDate format is 2016-02-11 01:00:00 +0000
NSString *dateString = [[self dateFormatter] stringFromDate:newDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"people%#",newDateString); //printing null
Easiest way
NSString *strDateFor = #"2016-02-11 01:00:00 +0000";
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZZ"];
NSDate *date = [dateformate dateFromString:strDateFor];
NSLog(#"date : %#",date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSString *strDate = [dateFormat stringFromDate:date];
NSLog(#"The strDate is - %#", strDate);
The Console output is
date : 2016-02-11 01:00:00 +0000
The strDate is - 11-02-2016
NSString *myString = #"2016-02-11 11:29:04";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"dd-MMM-yyyy";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
+0000 it the time zone definition. You should parse it with Z
try this
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ssZZZZ"];
else if your date is current date use
[dateformat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];

NSDate returns nil for 1933-01-01 00:00:00 in Singapore local time (SGT)

If you run this code you can see that the NSDate is always null for the 1933-01-01 00:00:00 +0000. Please help me how to resolve it.
NSString *date = #"1933-01-01 00:00:00 +0000";
NSString* dateString = [[NSString stringWithFormat:#"%#", date] stringByReplacingOccurrencesOfString:#" +0000" withString:#""];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"SGT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormatter setLocale:[NSLocale systemLocale]];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate* sgtDate = [dateFormatter dateFromString:dateString];
NSLog(#"SGT Date: %#",sgtDate);
Try
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+08:00"]];
Instead of
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"SGT"]];

Convert static time to NSDate returns older date

NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"hh:mm a"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setLocale:[NSLocale systemLocale]];
NSLog(#" >> %# >>> %#",date,[df dateFromString:#"12:18 AM"])
And log is
>> 2014-04-10 07:00:48 +0000 >>> 2000-01-01 06:52:00 +0000
I want that the second date has also the same date as the first.
like
2014-04-10 07:00:48 +0000 >>> 2014-04-10 06:52:00 +0000
If you want to convert the time string "12:18 AM" to a date for the current day then you can proceed as follows:
NSString *time = #"12:18 AM";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
// Get year-month-day for today:
[fmt setDateFormat:#"yyyy-MM-dd "];
NSString *todayString = [fmt stringFromDate:[NSDate date]];
// Append the given time:
NSString *todaysTime = [todayString stringByAppendingString:time];
// Convert date+time string back to NSDate:
[fmt setDateFormat:#"yyyy-MM-dd h:mm a"];
NSDate *date = [fmt dateFromString:todaysTime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
By using this formatter, you can get NSDate

Resources