How to convert a specific date format into a proper data type and query related records between dates? - datetime

I am learning neo4j and have a problem where a given data set I have uploaded has a weird date format which I can't query using neo4j's bult in date functions because it was uploaded as a string. The format is the following:
╒══════════════════════════╕
│"t.date" │
╞══════════════════════════╡
│"Mon 18 Feb 2019 12:18:57"│
├──────────────────────────┤
│"Mon 18 Feb 2019 12:18:57"│
└──────────────────────────┘
I have already created a node that contains date as a property and stores the dates in the above format.
How can I change this so I can query the associated node to return results BETWEEN certain dates, so for example:
MATCH (t:Text)
WHERE t.date = 'Mon 18 Feb 2019 12:18:57'
RETURN t.description;
I would need to be able to query for Texts in between certain dates for example texts written in between Mon 18 Feb 2019 12:18:57 and Mon 19 Feb 2019 12:18:57
Thank you!

There are two ways:
Change the existing date property to Neo4j 'DateTime'. Which can be easily queried. (RECOMMENDED)
Keep the date property as it is and use apoc to compare the date each time you want to query. (NOT RECOMMENDED)
You can use apoc.date.parse function from APOC Plugin to parse the date string into epoch time by specifying the SimpleDateFormat
You can use the following query to change your existing dates into Neo4j 'DateTime': (For Solution 1)
MATCH (n:Text)
WHERE n.date IS NOT NULL
SET n.date=datetime({epochmillis:apoc.date.parse(n.date, 'ms',"EEE dd MMM yyyy HH:mm:ss")})
Refer Neo4j DateTime
Note: Install APOC before running above query.

Once you convert the date string into datetime format, you can do below query to get text description when date is between Feb 18 and 19 12:18:57. Notice the letter 'T' at the middle. It means time.
MATCH (t:Text)
WHERE t.date > datetime('2019-02-18T12:18:57')
AND t.date < datetime('2019-02-19T12:18:57')
RETURN t.description;
Reference:
https://neo4j.com/docs/cypher-manual/current/functions/temporal/datetime/#functions-datetime-create-string

Related

Magnolia JCR-SQL2 order by Date

In the JCR, I've noticed that dates are stored in the format Feb 19, 2015 12:00:00 AM. This means that when you try and order a query by a date, it doesn't seem to work:
SELECT * FROM [mgnl:pages] ORDER BY articlePublishedDate
Will return:
Apr 1, 2015 12:00:00 AM
Dec 1, 2015 12:00:00 AM
Feb 1, 2015 12:00:00 AM
Is there any way to make the ORDER BY clause act as a integer? I've tried CAST(articlePublishedDate AS LONG) but it appears my content repository doesn't like it ...
This is more issue of JCR than Magnolia , however, one may do the following for working this around.
SELECT p.* FROM [mgnl:page] AS p
WHERE p.[mgnl:lastModified] > CAST('2016-06-10T07:24:50.233Z' AS DATE)
I assume order by also should work the same way.
Cheers
Make sure articlePublishedDate node property is of type Date, not String. For example, the following JCR2 query returned the results in the correct order when executed on the website repository:
select p.* from [mgnl:page] as p order by p.[jcr:created] desc
Ended up sorting in code, as it wasn't supported by my implementation of JCR.

Identifying what date this value represents in SQLite

I am brand new (!) to SQLite and will not be studying or using it long-term; however, I am trying to paw through a bit of archived data in a sqlite database using db browser for sqlite.
There is a table with a date field with a value like this: 1435610912000000
Does that make any sense to anyone as to a date of some kind ??
That is the number of microseconds from 1970 (epoch). Therefore, that is 1435610912000 milliseconds (or 1435610912 seconds), which converts to Mon Jun 29 2015 20:48:32 UTC using this website.
This can be a timestamp which every programming language has a function for converting it to a Date objec.
var date = new Date(1435610912000000);
This code above is Javascript that casts the number 1435610912000000 to date
Sat Sep 13 47462 01:53:20 GMT+0100 (WAT)
which is a bit off but the best guess is that it is a timestamp

SQLite timestamp conversion function

I've inherited a SQLite DB, in it I've a TIMESTAMP field called ZDATE.
One value is 401,580,000 and I know it correspond to Sept 23rd, 2013.
I've calculated that the 0 of this field is the midnight of Jan 1st, 2001 (?).
However, I didn't find any conversion function to get a formatted date, in fact if I use date() I get:
ZDATE date(zdate)
401580000 1094776-12632211-20
Any help will be appreciated.
> select 401580000 / (julianday('2013-09-23') - julianday('2001-01-01'));
86398.4509466437
> select 60*60*24;
86400
So this timestamp appears to use seconds.
To convert it into a timestamp that SQLite can use directly, i.e., a Unix epoch timestamp, just add the appropriate offset:
> select datetime(401580000 + strftime('%s', '2001-01-01 02:00:00'), 'unixepoch');
2013-09-23 00:00:00

SQLite Group by Month

I am using SQLite Database and in one my table has field purchased_date (TEXT ,since DATE is not in SQLLite)
Now I want to run query that return me all the results where user purchased in Month of February 2012
I am storing Dates in following format
Tue Mar 27 09:38:31 BST 2012
Is it possible to run query for above date format or do I need to put in different format ?
You can use the strftime built-in function to extract the month from the stored text value and group by this.
A full list of the datetime functions can be found here http://sqlite.org/lang_datefunc.html

Javascript ASP.net date format without timezone info - timezone offsets

I have a client side JavaScript that generates a date in JavaScript( new Date(2007,5,1)).
I need this date passed through to a hidden field that the code behind can access.
My issue is that when the hidden field is converted into a DotNet datetime, the time is incorrect. This is because the JavaScript is including timezone info from the client browser.
DotNet is then using this info to recalculate the time based on the difference between the server time and the client time.
What i need from the JavaScript is just the year, month and day.
I don't want to pass through 3 int values to my code behind as this will be a major change to the whole app.
What is the best way for me to accomplish this?
If i can set a UTC time with no timezone info I think that might work.
Any help is appreciated.
demo
If I understood it correctly,
you need .toDateString()
var date = new Date(2007,5,1);
document.write(date);
document.write("<br><br>versus<br><br>");
document.write(date.toDateString());
prints
Fri Jun 01 2007 00:00:00 GMT+0800 (Taipei Standard Time)
versus
Fri Jun 01 2007
You can use DateTimeOffset.ParseExact to parse a string to a DateTimeOffset value using the format you specify:
string dateString = "Fri Jun 01 2007 00:00:00 GMT+08:00";
DateTimeOffset date = DateTimeOffset.ParseExact(dateString, "ddd MMM dd yyyy hh:mm:ss 'GMT'zzz", CultureInfo.InvariantCulture);
You have to put GMT in quotes otherwise M will be interpreted as a format character.
Unfortunatelly, it is not possible to ignore part of the string value. If your string includes the name of the timezone you have to split it first and get the part without the description
string dateString = "Fri Jun 01 2007 00:00:00 GMT+08:00 (Taipei Standard Time)";
var parts=dateString.Split('(');
string datePart = parts[0].TrimEnd();
var date=DateTimeOffset.ParseExact(datePart,"ddd MMM dd yyyy hh:mm:ss 'GMT'zzz",CultureInfo.InvariantCulture);
You can build up a string from the javascript Date object you have created - it has getDate(), getMonth() and getFullYear() methods that you can use to build up the exact string you want in the hidden field.
I would recommend to use a format specification in C# when you get the values in the code behind file. Let me explain what I mean -
The date time format for the Date(...) in JavaScript is as follows
"Tue Jun 1 11:12:15 UTC+0530 2010"
which in C# would translate to the following format string -
"ddd MMM d hh:mm:ss UTCzzz yyyy"
with this format string use the DateTime.ParseExact(string <Hidden Field Value>, format, provider) to get the correct value for the datetime in C#.
Use provider as System.Globalization.CultureInfo.InvariantCulture.

Resources