I have to time value in output
First is :=
"updated_time" = 1308318422; (Unix timestamp, returning from "stream" table from facebook)
Second is :=
"created_at" = "Thu Jul 14 15:24:51 +0000 2011"; ( This is time of Twitter post, I don't what type of format it is).
I have to compare both date/time. I have to list most recent will come first and so on.
What is the logic? Code preference will be appreciate :)
Thanks in advance
Convert both values to NSDate objects and then use the compare: method.
The first can be converted by getting the int value of the updated_time and then using [NSDate dateWithTimeIntervalSince1970:updatedTimeInt].
The second can be converted with an NSDateFormatter object.
Related
I have 10 records in database. I want to fetch the record where date == mydate.
For eg, there are 2 records in database, which has date of 502479617.512 (in milliseconds).
I am converting the above date to NSDate using
[NSDate dateWithTimeIntervalSinceReferenceDate:502479617.512].
I am getting 2016-12-03 17:40:17 +0000
I have date parameter '502479617.500' which returns same NSDate 2016-12-03 17:40:17 +0000.
But when i try to fetch from coredata, i am getting 0 objects. It is because of milliseconds in the database. How can i discard milliseconds to fetch the records which has same date, time and seconds.
NSDate properties will be stored as NSDate attributes, unless the "Use scalar properties for primitive data types" checkbox is marked.
In that case, the core data attribute will be stored as a NSTimeInterval (double).
You can do it in either of two ways:
First, you could eliminate the seconds decimals when storing the NSDate. It could be automated by overriding didSet for the property. You could also simply do it wherever you do the conversion you describe.
Alternatively, you could specify an interval of one second when fetching. Your fetch request would then need a predicate of this kind:
NSPredicate(format: "date >= %# && date < %#", aDate, aDate.addingTimeInterval(60))
Objective-C
[NSPredicate predicateWithFormat:#"date >= %# && date < %#",
aDate, [aDate dateByAddingTimeInterval:60]];
Just save it as NSString. Then convert back to double when you need it.
I am having an OData Service returning some DateTime values. They are saved in a table in the back end as TIMESTAMPL (with some other data).
Now there is the value 20160630084459.5000. With MOVE-CORRESPONDING into the et_entityset, where it is a TIMESTAMP. Because of the rounding, it gets 20160630084460, Since a the seconds must be between 00 and 59, this is not a valid value to return.
My main problem is, that my table has extremely much entries, so I need a performant way to fix this error.
Here is a way to convert it to what you want.
REPORT zzy NO STANDARD PAGE HEADING.
FORM convert_timestamp.
DATA(l_t1) = CONV timestampl('20160630084459.5000').
DATA: l_t2 TYPE timestamp.
l_t2 = l_t1.
WRITE / : l_t1, l_t2.
CONVERT TIME STAMP l_t1 TIME ZONE sy-zonlo INTO DATE DATA(l_date) TIME DATA(l_time).
CONVERT DATE l_date TIME l_time INTO TIME STAMP l_t2 TIME ZONE sy-zonlo.
WRITE / l_t2.
ENDFORM.
START-OF-SELECTION.
PERFORM convert_timestamp.
Here is the output.
20.160.630.084.459,5000000
20.160.630.084.460
20.160.630.084.459
You mention floor in your question but that is not what is happening. The value is rounded. If you simple do use FLOOR in your assignment from TIMESTAMPL to TIMESTAMP you will get the answer you want. If you have to use MOVE-CORRESPONDING, just do that first and then do a seperate assignment for the timestamp.
However, this means that 0:59.9 will get translated to 0:59 and not 1:00. If that missing second is OK for your application then just use the FLOOR command. If not it becomes more complicated and you will take a performance hit.
I have this query
https://api.backendless.com/v1/data/abc?pageSize=100&loadRelations=cinema&sortBy=showing%20asc&where=objectId%3D%272C225111-18FC-DD4F-FF40-E227F49F5B00%27%20AND%20showing%20%3E%20%2714-June-2016%27
Thi query rightly fetches all the results after the date 14 June 2016. But I am giving a hard coded date here, Is there any way I can simply add current time/date here so that It brings all the results prior to current time? I am looking for something like
https://api.backendless.com/v1/data/abc?pageSize=100&loadRelations=cinema&sortBy=showing%20asc&where=objectId%3D%272C225111-18FC-DD4F-FF40-E227F49F5B00%27%20AND%20showing%20%3E%20%CURRENT TIME%27
I have searched alot, and even the documentation refers to hardcode dates or millisecond format only. Any help is appreciated
The easiest option is just to use the UNIX timestamp version of the date, so the string to use in your where clause would be like:
NSString *where = [NSString stringWithFormat:#"showing < %#", [[NSDate date] timeIntervalSince1970]];
This question already has answers here:
NSDate isEqualToDate: not working - does it look at seconds and split seconds?
(3 answers)
Closed 8 years ago.
i compare 2 NSDates which are the same and i get false result.
i cant show how i get this dates because its too long , but i can show what i do :
NSLog(#"this date is:%# , and date we check to equality is:%#",thisDate,dateToFind);
if([thisDate isEqualToDate:dateToFind] )
{
NSLog(#"equal date!"); // not printed!
}
the NSLog show me this :
this date is:2012-09-13 14:23:54 +0000 , and date we check to equality is:2012-09-13 14:23:54 +0000
he doesnt print the NSLog .
why ?
As a few have said it seems to be the fractions of a second that are giving you trouble. The reason for this is that an NSDate is simply an object wrapper around an NSTimeInterval(double) with a value in seconds since the reference date(12AM January 1 2001 GMT).
There are a couple main ways to deal with this. Either check the date to see if it is in a given range, or (more likely based on your question) truncate the fractions of a second completely off.
Truncating seconds from an NSDate is trivial code. You may want to truncate all of the dates that you are storing as you store them for quick comparisons. You can truncate an existing NSDate like this:
NSDate *truncatedDate = [NSDate dateWithTimeIntervalSinceReferenceDate:((NSTimeInterval)lround(originalDateObject.timeIntervalSinceReferenceDate))];
This code is pretty self explanatory. It grabs the date's backing time interval rounds it to an integer casts that back to a time interval and creates a new truncated date.
Once you do this to both dates you can then compare your two truncated dates and they will behave as expected.
Or if you must do something without changing your date data you could simply do:
if (lround(thisDate.timeIntervalSinceReferenceDate) == lround(dateToFind.timeIntervalSinceReferenceDate)){
// If whole seconds are equal, as shown in log, this will execute.
}
NSLog(#"this date is:%# , and date we check to equality is:%#",
thisDate,dateToFind);
Try changing the above with:
NSLog(#"this date is:%f , and date we check to equality is:%f",
[thisDate timeIntervalSince1970],
[dateToFind timeIntervalSince1970]);
I'm sure this is quite simple, but I've been stuck on it for some time. How can I convert a varchar field (YYYYMM) to a date (MM/01/YY) in SQL?
Thanks.
Edit: I'm using Open Office Base (HSQL), not MySQL; sorry for the confusion.
Try the str_to_date and date_format functions. Something like:
select date_format( str_to_date( my_column, '%Y%c' ), '%c/01/%y' ) from my_table
try :
SELECT STR_TO_DATE(CONCAT(myDate,'01'),'%Y%m%d')
FROM myTable
Use STR_TO_DATE:
From mysql.com:
STR_TO_DATE(str,format)
This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.
The date, time, or datetime values contained in str should be given in the format indicated by format. For the specifiers that can be used in format, see the DATE_FORMAT() function description. If str contains an illegal date, time, or datetime value, STR_TO_DATE() returns NULL. Starting from MySQL 5.0.3, an illegal value also produces a warning.
Range checking on the parts of date values is as described in Section 11.3.1, “The DATETIME, DATE, and TIMESTAMP Types”. This means, for example, that “zero” dates or dates with part values of 0 are allowed unless the SQL mode is set to disallow such values.
mysql> SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');
-> '0000-00-00'
mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
-> '2004-04-31'
Get the year:
SUBSTRING(field FROM 2 FOR 2)
Get the month:
SUBSTRING(field FROM -2 FOR 2)
Compose the date:
CONCAT(SUBSTRING(field FROM -2 FOR 2), '/01/', SUBSTRING(field FROM 2 FOR 2))
This will convert from YYYYMM to MM/01/YY.
To be clear: if you're looking for method to convert some value of type Varchar/Text to value of type Date than solutions are:
using CAST function
CAST(LEFT('201205',4)||'-'||SUBSTRING('201205' FROM 5 FOR 6)||'-01' AS DATE)
starting from OpenOffice 3.4 (HSQLDB 2.x) new Oracle-like function TO_DATE supposed to be available
TO_DATE('201205','YYYYMM')
in addition to the written i can mention that you also can construct a string with ANSI/ISO 'YYYY-MM-DD' formatted representation of the date,- Base will acknowledge that and succesfully convert it to the Date type if necessary (e.g. INSERTing in Date typed column etc.)
Here is doc's on HyperSQL and highly recommended OO Base guide by Andrew Pitonyak