I have a problem while retrieving DateTime from sqlite3.
For Example to retrieve an int value we use
sqlite3_column_int(selectstmt, 1);
In the same way what function I have to use to retrieve the DateTime and how to convert it to NSDate.
You have to retrieve it as text sqlite3_column_text, and convert it to date, using proper date format, if you want.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
char* value = (char *)sqlite3_column_text(selectStatement, 2);
if(value != nil){
NSString *dateTest = [NSString stringWithUTF8String:value];
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; //sample date format
NSString *formattedDateString = [dateFormatter stringFromDate:matchDate];
}
Related
I'm having an odd problem with converting a datetime that is in the UTC timezone when this code is run in Sweden.
NSString* utcDateTime = #"5/23/2017 4:34 AM";
NSString* localTimeNSString = [self convertUTCDateTimeToLocal:utcDateTime];
NSLog(#"localTime:%#", localTimeNSString);
Inside my conversion function, my attempt to parse this datetime into an NSDate returns nil:
- (NSString*)convertUTCDateTimeToLocal:(NSString*)theUTCDateTime
{
NSString* localTime = nil;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
if (dateFormatter) {
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
// parse the passed parameter, theUTCDateTime, which looks like this: "05/23/2017 04:34 AM"
// Even though this date / time doesn't LOOK like a UTC-formatted timestamp, it is in the UTC timezone
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate* utcTime = [dateFormatter dateFromString:theUTCDateTime];
// in Sweden, dateFormatter will return utcTime as nil. Not so in the United States or England.
// In the US, NSLog of utcTime is "2017-05-23 04:34:00 +0000"
// Now that we've parsed into an NSDate, display date & time the way the user's system is configured
// Of course, if utcTime is nil, then so is localTime (below)
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
dateFormatter.dateStyle = NSDateFormatterShortStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
localTime = [dateFormatter stringFromDate:utcTime];
// in the US, dateFormatter will return localTime as "5/22/17, 9:34 PM"
}
return localTime;
}
NSDateFormatter uses the current locale. Set the locale of the formatter to a US locale if you want to recognize dates in a US format.
Restkit is encoding my date objects, so that the url it produces looks like this:
appointments?enddate=2013-12-21T04%3A00%3A00.000&startdate=2013-12-21T04%3A00%3A00.000
However, I need them to look like this:
appointments?enddate=2013-12-20T21:00:00Z&startdate=2013-12-10T09:00:00Z
Here's my code..
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
[RKObjectManager.sharedManager getObjectsAtPath:#"appointments" parameters:#{
#"startdate" : [dateFormatter stringFromDate:startDate],
#"enddate" : [dateFormatter stringFromDate:endDate],
}
success:
^(
RKObjectRequestOperation *operation, RKMappingResult
*mappingResult) {
if (blockSelf.dayButton.isSelected)
{
blockSelf.dayView.appointmentsForTimeRange = mappingResult.array;
} else
{
blockSelf.weekView.appointmentsForTimeRange = mappingResult.array;
}
}
failure:
^(
RKObjectRequestOperation *operation, NSError
*error) {
LogVerbose(#"error %#", error);
}];
RestKit is doing exactly what you're asking it. It isn't changing the date format. %3A is the URL encoded version of colon. If you need the date format to be different then you need to change the date format string. Perhaps to:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
(Check the date formatted spec for exactly what to use).
I am new to xcode and object oriented development (was databasic back in the day...) and am trying to build a string based on whether or not the start date is the same as the end date, so if it is the same just return the start date and if not return the start and end date (i.e. 15/02/2013 - 17/02/2013). My code is as follows but it does not like the 'fulldatestring', is there something wrong with my if statement?
NSString *startDate = [info objectForKey:#"StartDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd"];
NSDate *sdate = [dateFormatter dateFromString:startDate];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/mm/yyyy"];
NSString *convertedStartDate = [dateFormatter stringFromDate:sdate];
NSString *endDate = [info objectForKey:#"EndDate"];
[dateFormatter setDateFormat:#"yyyy-mm-dd"];
NSDate *edate = [dateFormatter dateFromString:endDate];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/mm/yyyy"];
NSString *convertedEndDate = [dateFormatter stringFromDate:edate];
if(convertedStartDate isEqualToString:convertedEndDate) {
NSString *fulldatestring = convertedStartDate;
}
else
NSString *fulldatestring = [convertedStartDate, convertedEndDate ];
{
cell.datesLabel.text = fulldatestring
To compare NSDates, see the NSDate instance methods isEqualToDate: and compare:. You don't need to (and shouldn't) convert them to NSStrings to compare them.
Search the NSString class reference for "append" and you'll see two methods that append one NSString to another.
That should put you in the right direction, but I can post code if you really need it.
There are a couple issues with your Objective-C syntax.
Whenever sending a message to an object such a isEqualToString: to a string it must be in square brackets. as well your braces around the if statement don't match.
if([convertedStartDate isEqualToString:convertedEndDate]) {
NSString *fulldatestring = convertedStartDate;
} else {
NSString *fulldatestring = [NSString stringWithFormat:#"%# - %#", convertedStartDate, convertedEndDate];
}
As well NSString objects are not mutable so you cannot just concat them like you are trying to do you must create a new NSString object.
Hey I'm having problems formatting my date ....
my code it this :
NSString *dateString = news.date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.DateFormat =#"yyyy-MM-dd'T'HH:mm:ss";
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString: dateString];
NSLog(#" %#", dateFromString);
if i look at my dateString it´s 2012-03-06T12:15:45
but when i format it, it comes out 2012-03-06 11:15:45 +0000
how do i remove the +0000 ???
dateFromString is an NSDate instance. So 2012-03-06 11:15:45 +0000 is the correct result you get when you call NSLog(#" %#", dateFromString);. That's simply how the method description of NSDate works.
If you want to change a string of type 2012-03-06T12:15:45 to a string of type 2012-03-06 11:15:45, then you have to create another string fromdateFromString` like the following:
// ...
dateFromString = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSString *newDateString = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", newDateString);
I basically need to get current date and time separately, formatted as:
2009-04-26
11:06:54
The code below, from another question on the same topic, generates
now: |2009-06-01 23:18:23 +0100|
dateString: |Jun 01, 2009 23:18|
parsed: |2009-06-01 23:18:00 +0100|
This is almost what I'm looking for, but I want to separate the day and time.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd, yyyy HH:mm"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"MMM dd, yyyy"];
NSDate *parsed = [inFormat dateFromString:dateString];
NSLog(#"\n"
"now: |%#| \n"
"dateString: |%#| \n"
"parsed: |%#|", now, dateString, parsed);
this is what i used:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
[dateFormat release];
[timeFormat release];
[now release];
iPhone format strings are in Unicode format. Behind the link is a table explaining what all the letters above mean so you can build your own.
And of course don't forget to release your date formatters when you're done with them. The above code leaks format, now, and inFormat.
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"Your Date Format"];
set the format to return is....
yyyy-MM-dd return 2015-12-17 date
yyyy-MMM-dd return 2015-Dec-17 date
yy-MM-dd return 15-12-17 date
dd-MM-yy return 17-12-15 date
dd-MM-yyyy return 17-12-2015 date
yyyy-MMM-dd HH:mm:ss return 2015-Dec-17 08:07:13 date and time
yyyy-MMM-dd HH:mm return 2015-Dec-17 08:07 date and time
For more Details Data and Time Format for Click Now.
Thank you.....
you can use this method just pass your date to it
-(NSString *)getDateFromString:(NSString *)string
{
NSString * dateString = [NSString stringWithFormat: #"%#",string];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"your current date format"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"your desired format"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
NSLog(#"%#", stringFromDate);
return stringFromDate;
}
nothing new but still want to share my method:
+(NSString*) getDateStringFromSrcFormat:(NSString *) srcFormat destFormat:(NSString *)
destFormat scrString:(NSString *) srcString
{
NSString *dateString = srcString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateFormat:#"MM-dd-yyyy"];
[dateFormatter setDateFormat:srcFormat];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
//[dateFormatter setDateFormat:#"yyyy-MM-dd"];
[dateFormatter setDateFormat:destFormat];
NSString *newDateString = [dateFormatter stringFromDate:date];
return newDateString;
}
For swift
var dateString:String = "2014-05-20";
var dateFmt = NSDateFormatter()
// the format you want
dateFmt.dateFormat = "yyyy-MM-dd"
var date1:NSDate = dateFmt.dateFromString(dateString)!;
Swift 3
extension Date {
func toString(template: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: NSLocale.current)
return formatter.string(from: self)
}
}
Usage
let now = Date()
let nowStr0 = now.toString(template: "EEEEdMMM") // Tuesday, May 9
let nowStr1 = now.toString(template: "yyyy-MM-dd") // 2017-05-09
let nowStr2 = now.toString(template: "HH:mm:ss") // 17:47:09
Play with template to match your needs. Examples and doc here to help you build the template you need.
Note
You may want to cache your DateFormatter if you plan to use it in TableView for instance.
To give an idea, looping over 1000 dates took me 0.5 sec using the above toString(template: String) function, compared to 0.05 sec using myFormatter.string(from: Date).
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"]
NSString *dateString = [df stringFromDate:date];
[df setDateFormat:#"hh:mm:ss"];
NSString *hoursString = [df stringFromDate:date];
Thats it, you got it all you want.