Subtracting and converting NSString and NSDate - ios

I have the following code:
NSString *startDate = #"2014-09-29 05:46:34 +000";
// Format the string
NSRange range = NSMakeRange(0, 19);
startDate = [startDate substringWithRange:range];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:startDate];
NSDate *trendDate = [dateFromString dateByAddingTimeInterval:-3600*12];
NSString *dateString = [NSString stringWithFormat:#"%#", trendDate];
My date string then ends up as:
#"2014-09-29 00:46:34 +000";
Why is it only minusing five hours instead of 12?

Instead of getting the substring from startDate string, try this date format:
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss ZZZ"
Then
dateFromString = [dateFormatter dateFromString:startDate];
NSDate *trendDate = [dateFromString dateByAddingTimeInterval:-3600*12];
NSString *dateString = [dateFormatter stringFromDate:trendDate];

Its because you are removing timezone from the date string that why you are getting wrong date
instead of removing +000 from your date string just replace your date formate.
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
With
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
Removed code
NSRange range = NSMakeRange(0, 19);
startDate = [startDate substringWithRange:range];
Final Code
NSString *startDate = #"2014-09-29 05:46:34 +000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:startDate];
NSDate *trendDate = [dateFromString dateByAddingTimeInterval:-(3600*12)];

Related

Convert date to string with different date format ios?

I search some stack quentions for converting string to date and that's working fine, but i convert my string date to NSDate than it's return me nil. Here is my code :
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
I want to convert this date to yyyy-MM-ddTHH:mm:ss.SSSZ this formate.
What's problem here ?
do like
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// set the date format related to what the string already you have
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
// again add the date format what the output u need
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *finalDate = [dateFormat stringFromDate:date];
NSLog(#"finalDate==%#",finalDate);
output
Your problem is that your date format #"yyyy-MM-ddTHH:mm:ss.SSSZ" doesn't match your date string #"2016-07-29 09:05". You need to use a different date format.
Working copy -
Objective C
NSString *dateStr = #"2016-07-29T09:05:00.00000Z";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
Swift 2.x
var dateformater = NSDateFormatter()
dateformater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
var dateString:String = "2016-07-29T09:05:00.00000Z"
let date:NSDate = dateformater.dateFromString(dateString)!
print(date)
Try this code.
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"the date is %#",date);
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSString *currentDate = [dateFormatter stringFromDate:date];
NSDate *finaldate = [dateFormatter dateFromString:currentDate];
NSLog(#"CurrentDate:%#", finaldate);
Just change your formatterstyle as below
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
After that you can convert the date into below format :
dateFormat.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
NSString *str = [dateFormat stringFromDate:date];
NSLog(#"%#",str);

Convert date from weird string to date in format day month and year

Hi I am parsing an end point of API, from there I am getting a string which contain like this date string 2016-01-18T13:28:06.357 but I want to convert this like 01 Jan 16, I have tried many times by using different format one of following mentioned. kindly need suggestion how to accomplish this task.
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSDate *myDate = [dateformat date];
// Objective C
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *myDate = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSString *strDate=[dateformat stringFromDate:myDate];
// Swift 3.0
var date = (dictionary["date"] as! String)
var dateformat = DateFormatter()
dateformat.timeZone = NSTimeZone(name: "EST")
dateformat.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
var myDate = dateformat.date(fromString: date)!
dateformat.dateFormat = "dd MMM yyyy"
var strDate = dateformat.string(from: myDate)
You should use this format:
NSString *dateStr = #"2016-01-18T13:28:06.357";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [formatter dateFromString:dateStr];
// date is 2016-01-18 08:28:06 +0000
And next convert that date to format you need
You can also use this :
NSString *dateString = #"2016-01-18T13:28:06.357";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:#"dd MMMM yy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"newDateString=%#",newDateString);
You should use this format code
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//Get Date
[formatter setDateFormat:#"dd"];
NSString *strDate = [formatter stringFromDate:[NSDate date]];
NSLog(#"Date is - %#",strDate);
//Get Month
[formatter setDateFormat:#"MMM"];
NSString *strMonth = [formatter stringFromDate:[NSDate date]];
NSLog(#"Month is - %#",strMonth);
//Get Year
[formatter setDateFormat:#"yyyy"];
NSString *strYear = [formatter stringFromDate:[NSDate date]];
NSLog(#"Year is - %#",strYear);
Log is :
Date is - 05
Month is - Feb
Year is - 2016
for set more format, Click Me

Converting NSDate to NSString and NSdate of another Format

I am using following method to convert NSDate to NSString
- (void)splitDateintoDateTime:(NSDate *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
strDate = [formatter stringFromDate:date];
[formatter setDateFormat:#"hh:mm a"];
strTime = [formatter stringFromDate:date];
}
The format of date is yyyy-MM-dd hh:mm:ss z
I want separate NSDate values for Date and Time with format mentioned in the method.
- (void)splitDateintoDateTime:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
dateFormatter.dateFormat = #"dd/MM/yyyy";
NSString *dateString = [dateFormatter stringFromDate: date];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = #"HH:mm z";
NSString *dateString = [timeFormatter stringFromDate: date];
}

How to Fetch date and time from a formatted string in iOS

I have a NSString "dateString" which contains date and time in given format.
"MM/dd/yyyy HH:mm:ss a".
I want to fetch the data from the dateString using NSDateFormatter and then I'm trying to show in a String "strDate". I.m able to fetch the data but the only issue is that i'm not getting the correct value for "hour" place.
For Ex:
dateString = 1/23/2015 7:06:37 AM
strDate = 01/23/2015 00:06:37 am
How to resolve this issue. Thanks in advance. This is my code:
-(NSString *)getDateQuestion:(NSString *)dateString
{
NSLog(#"dateString = %#",dateString);
//NSString to NSDate
NSDate *datefromString = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT+5:30"]];
[dateFormatter setTimeZone: [NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
datefromString = [dateFormatter dateFromString:dateString];
NSLog(#"dateString1 = %#",datefromString);
//NSDate convert to NSString
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT+5:30"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *strDate = [dateFormatter stringFromDate:datefromString];
NSLog(#"strDate = %#",strDate);
return strDate;
}
Just Pass Correct date Time string to the function and don't set time zone Unnecessary again and again... i hope it helps you...
NSDate *dat = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC+05:30"]];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSString * dateString =[dateFormatter stringFromDate:dat];
NSLog(#"dateString = %#",dateString);
NSLog(#"dateString1 = %#",dateString);
//NSDate convert to NSString
[dateFormatter setDateFormat:#"HH:mm:ss a"];
NSString *strDate = [dateFormatter stringFromDate:dat];
NSLog(#"strDate = %#",strDate);
HH stands for 24hr.
hh satnds for 12 hr.
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
Replace with "MM/dd/yyyy hh:mm:ss a"

IOS date returns null

I have date in object "start_time"(2000-12-11T00:00:00+0000), but i need to print date and time separate to textfields. But when i tried with this code i got null value on console
__block NSDictionary *events = [[e.response objectForKey:#"events"] objectAtIndex:0];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// NSDate *dates = [events objectForKey:#"start_time"];
// NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] init];
dateFormatter.dateFormat = #"MMMM dd yyyy";
NSString *dateString = [dateFormatter stringFromDate: [events objectForKey:#"start_time"]];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] init];
timeFormatter.dateFormat = #"HH:mm:ss ZZZZ";
NSString *timeString = [timeFormatter stringFromDate: [events objectForKey:#"start_time"]];
NSLog(#"This is time------------------------------: %#",dateString); // null returns
//
This is how i store date into "start_time" by textfields
Blockquote
NSString *datestring = self.date.text;
NSString *timestring = self.time.text;
NSString *combined = [NSString stringWithFormat:#"%# %#", datestring, timestring];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:05.30]];
[dateFormat setDateFormat:#"MM dd yyyy HH:mm a"];
NSDate *date = [dateFormat dateFromString:combined];
NSDictionary *data = #{
#"start_time":[NSString stringWithFormat:#"%#",date]};
I think that became null Because you upcoming date from event that is different format and you are first setting format is different.
dateFormatter.dateFormat = #"MMMM dd yyyy";
that have to:
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
You have to set first current date format then you can change it your want date.
Your setting an incorrect format for the date,
Try the below, this will work
//"myDate" variable is an example string, you can change it with "[events objectForKey:#"start_time"]"
NSString *myDate = #"2000-12-11T00:00:00 +0000";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] init];
dateFormatter.dateFormat = #"yyyy-dd-mm'T'HH:mm:ss Z";
NSDate *myDateObject = [dateFormatter dateFromString:myDate]; //Now you have the date object that return the correct value
//FOR date
[dateFormatter setDateFormat:#"yyyy-dd-mm"];
NSString *OnlyDate = [dateFormatter stringFromDate:myDateObject];
//For Time
[dateFormatter setDateFormat:#"HH:mm:ss Z"];
NSString *OnlyTime = [dateFormatter stringFromDate:myDateObject];
NSLog(#"Date is %# and Time is %#",OnlyDate,OnlyTime);
UPDATE
In your case, if [events objectForKey:#"start_time"] is a NSDATE object then try the following
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] init];
dateFormatter.dateFormat = #"yyyy-dd-mm'T'HH:mm:ss Z";
NSDate *myDateObject = [dateFormatter dateFromString:[events objectForKey:#"start_time"]]; //Now you have the date object that return the correct value
//FOR date
[dateFormatter setDateFormat:#"yyyy-dd-mm"];
NSString *OnlyDate = [dateFormatter stringFromDate:myDateObject];
//For Time
[dateFormatter setDateFormat:#"HH:mm:ss Z"];
NSString *OnlyTime = [dateFormatter stringFromDate:myDateObject];
NSLog(#"Date is %# and Time is %#",OnlyDate,OnlyTime);
Hope this helps.

Resources