How to convert this type of Date Format to yyyy-MM-dd HH:MM:ss format.
"DATE_OF_JOINING":"\/Date(1349634600000+0530)\/"
I'd prefer a scanner over regular expressions to extract the time interval from the string
NSDictionary *dict = #{#"DATE_OF_JOINING" : #"Date(1357683534626+0530)"};
NSString *string = dict[#"DATE_OF_JOINING"];
NSString *intervalString;
NSString *offsetString;
NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner setScanLocation:[string rangeOfString:#"("].location+1];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:#"+-"] intoString:&intervalString];
[scanner scanUpToString:#")" intoString:&offsetString];
NSInteger hours = [[offsetString substringToIndex:3] integerValue];
NSInteger minutes = [[offsetString substringFromIndex:3] integerValue];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
offsetComponents.hour = hours;
offsetComponents.minute = minutes;
NSTimeInterval interval= [intervalString doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval/1000];
date = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:date options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *result = [formatter stringFromDate:date];
use NSDateFormatter. This will let you format it many different ways, and into strings.
Here's apple's reference on the class for iOS:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Related
I have two user selected dates: startDate and endDate. They are NSDate instances and I have to send them as parameters as NSNumbers. How can I convert them to NSNumber with seconds?
Use below code :
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
// set format however you want
[formatter setDateFormat:#"ddMMyyyy"];
NSDate *date = [NSDate date];
NSString *string = [formatter stringFromDate:date];
NSNumber *num1 = #([string intValue]);
NSLog(#"%#",num1);
1) First, get the date in MM/dd/yyyy format:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
2) get string date and remove '/' from it:
NString *string = [formatter stringFromDate:date];
NString *finalStr = [string stringByReplacingOccurrencesOfString:#"/" withString:#""];
3) Use NSNumberFormatter from converting NSString to NSNumber:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *myNumber = [f numberFromString: finalStr];
Hope, this is what you want!
If you mean a timestamp format
NSDate *date = [NSDate date];
NSTimeInterval ti = [date timeIntervalSince1970];
How to convert NSDate into Unix timestamp in Objective C/iPhone?
Thanks,
Hi I need to convert this string
"2013-12-05T08:58:55.9345456+02:00"
to NSDate
I am trying next format but without luck
"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"
Code below.
Can you help me with right format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// "2013-12-05T08:58:55.9345456+02:00"
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
Your format is close. You need:
yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ
And be sure to set the formatter's locale to en_US_POSIX. This should always be done when parsing fixed formatted, non-localized strings.
NSString * dateStr = #"2013-12-05T08:58:55.9345456+02:00";
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *timePart = [dateStrParts objectAtIndex:1];
NSString *t = [[timePart componentsSeparatedByString:#"."] objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%# %#",datePart,t];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // Change here for your formated output
NSDate *date = [df dateFromString:newDateStr];
NSLog(#"%#",date);
Output
2013-12-05 08:58:55 +0000
The problem lies in the timezone where you have +02:00.
The ZZZZ only parses to +0000, hence you are getting nil.
You can do in this way:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString *jsonDateString = #"2013-12-05T08:58:55.9345456+02:00";//#"2013-12-05T08:58:55.9345456+02:00"
NSRange lastColon = [jsonDateString rangeOfString:#":" options:NSBackwardsSearch];
if(lastColon.location != NSNotFound) {
jsonDateString = [jsonDateString stringByReplacingCharactersInRange:lastColon withString: #""];
}
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'.'SZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
NSLog(#"%#",date)
EDIT:
As per rmaddy's comment, you should use SSSSS instead of single S.
I need a NSString with the format "yyyy-mm-dd - 11:00:00"
Year, month, and date should be from the current date, while hour, minute, and second should be 11,00,00. I could not figure how to do that.
I think you mean yyyy-MM-dd - HH:mm:ss (because mm is minutes and MM is month). But you just need
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd - HH:mm:ss";
NSString *string = [formatter stringFromDate:[NSDate date]];
Or if you want it to say 11:00:00, either pass it that NSDate, or you could:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd' - 11:00:00'";
NSString *string = [formatter stringFromDate:[NSDate date]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd";
NSString *string = [NSString stringWithFormat:#"%# - 11:00:00", [formatter stringFromDate:[NSDate date]]];
I have two strings.
NSString *dateof = #"Mon May 21";
NSString *timeof = #"01.00 pm from 11.50 pm"; // 01.00 pm is the start time and 11.50 pm is the end time
I need to save these as NSDates, in the format , so that it will be 2012-05-21 13:00:00 +0000.
My approach so far has been:
NSDate *currentTime = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents
*currentDateComps = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:currentTime];
NSString *startDateStringWithYear = [NSString stringWithFormat:#"%# %d", startDateString, currentDateComps.year];
NSString *endDateStringWithYear = [NSString stringWithFormat:#"%# %d", endDateString, currentDateComps.year];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MM dd yyyy"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *startDate = [formatter dateFromString:startDateStringWithYear];
NSArray *timeDurationStringWords = [timeof componentsSeparatedByString:#" "];
// Calculate NSDate's for the start time for today:
NSString *startTimeString = [timeDurationStringWords objectAtIndex:0];
currentDateComps.hour = [[startTimeString substringToIndex:2] intValue];
currentDateComps.minute = [[startTimeString substringFromIndex:3] intValue];
if ([[timeDurationStringWords objectAtIndex:1] isEqualToString:#"pm"])
{
currentDateComps.hour += 12;
}
NSDate *startTime = [cal dateFromComponents:currentDateComps];
When I debug the code, startTime prints as 2012-05-29 07:30:00 +0000 which is incorrect (both the day and time are incorrect). I think this is because of the GMT time.
I need the date and time to be 2012-05-21 13:00:00 +0000.
This will do it. As far as the local/GMT, this will give you a NSDate that contains the local time but the debugger will show always show it in GMT. You will need to format it (using a NSDateFormatter) into the format that you want if you are going to show it to the user, in which case it will be in local time.
NSString *dateof = #"Mon May 21";
NSString *timeof = #"01.00 pm from 11.50 pm";
NSDate *today = [NSDate date];
// Start time will be array index 0, end time will be index 1:
NSArray *times = [timeof componentsSeparatedByString:#" from "];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *yearComp = [cal components:NSYearCalendarUnit fromDate:today];
// Append the year and the time to the dateof string to get a string like #"Mon May 21 2012 01.00 pm":
NSString *start = [dateof stringByAppendingFormat:#" %d %#", yearComp.year, [times objectAtIndex:0]];
NSString *end = [dateof stringByAppendingFormat:#" %d %#", yearComp.year, [times objectAtIndex:1]];
// Make a date formatter that recognizes the above date/time:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd yyyy hh.mm a"];
// Convert the strings to dates:
NSDate *startDate = [formatter dateFromString:start];
NSDate *endDate = [formatter dateFromString:end];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
NSString *str = [formatter stringFromDate:[NSDate date]];
NSLog(#"str=%#",str);
NSDate *dt = [NSDate date];
dt = [formatter dateFromString:str];
NSLog(#"dt=%#",dt);
Output:
str=2012-05-29 at 10:36
dt=2012-05-29 05:06:00 +0000
All,
I have a method that takes a date (YYYY-MM-DD H:M:S) from the database and creates a URL with the year, month and day components from the date string. My solution works but I was wondering if there is a better way to do this? How are the cool kids doing it? :-)
Thanks!
-(NSString *) prettyURLFrom:(NSString *)dateString{
NSString * URLString = #"";
NSString *URL = #"http://www.theblues.com/featured/article/";
NSArray *myWords = [dateString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#"-\" \" :"]
];
URLString = [NSString stringWithFormat:#"%#%#/%#/%#",
URL,
[myWords objectAtIndex:1],
[myWords objectAtIndex:2],
[myWords objectAtIndex:0]];
return URLString;
}
Like I said in the comments, I find it bizarre you're passing and returning values as strings when they probably should be NSDate and NSURL respectively.
I would probably do something like:
-(NSURL*)URLFromDate:(NSDate*)date
{
// Assumes user uses a gregorian calendar
NSDateComponents *dateComp = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
return [NSURL URLWithString:[NSString stringWithFormat:#"http://www.theblues.com/featured/article/%li/%li/%li"
, [dateComp year]
, [dateComp month]
, [dateComp day]]];
}
If you have NSDate object then you can use
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
NSString to NSDate
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:#"1999-07-02 2:2:4"];