Couchbase convert epoch to ISO - couchbase

I'm struggling to convert Epoch to ISO datetime with Couchbase N1QL. Couchbase stores etoch time with milliseconds and I need full ISO8 format YYY-MM-DDThh:mm:ss.S. I have tried:
SELECT DATE_PART_MILLIS(1463284740000, 'year'), DATE_PART_MILLIS(1463284740000, 'month'), DATE_PART_MILLIS(1463284740000, 'day')
but it obviously returns json-like response. I tries to concatenate like SELECT DATE_PART_MILLIS(1463284740000, 'year') || DATE_PART_MILLIS(1463284740000, 'month') || DATE_PART_MILLIS(1463284740000, 'day')
But it renders nothing back
[
{
"$1": null
}
]
Any suggestion?

In Couchbase you can store the time as number (In Milliseconds)
or string as ISO-8601 format. It provides many date functions to work on
ISO-8601 format as described at
https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/datefun.html
SELECT MILLIS_TO_STR(1463284740000);
DATE_PART_MILLIS(date1, part [, tz])
Extracts the value of a given date component from an Epoch/UNIX timestamp value.
Return Value -- An integer representing the value of the component extracted from the timestamp.
String concatenation expects string, Add TOSTRING around the function like
SELECT TOSTRING(DATE_PART_MILLIS(1463284740000, 'year')) || "-" ||
TOSTRING(DATE_PART_MILLIS(1463284740000, 'month')) || "-" ||
TO_STRING(DATE_PART_MILLIS(1463284740000, 'day'));

Related

Can't execute string to date conversion

Could anyone point what to read, because I'm stuck with trivial issue. I store data in JSON format and I have a key with string that consists of a non ISO format datetime value, e.g. '%d-%m-%Y %H:%i:%S'
I've tried function STR_TO_DATE on string "17-12-2019 13:49:50" and it returned SQL datetime result, but when I use it in function
DELETE FROM queue WHERE
ts = CASE
WHEN type = 'entry' THEN HOUR(TIMEDIFF (NOW(), STR_TO_DATE(JSON_EXTRACT(request, "$.date_event"),'%d-%m-%Y %H:%i:%S'))) > 72
SQL raises
Incorrect datetime value: '"07-12-2016 18:21:59"' for function
str_to_date
When I try statement SELECT STR_TO_DATE(JSON_EXTRACT(request, "$.date_event"),'%d-%m-%Y %H:%i:%S') MySQL returns null
Your issue is caused by the double quotes returned around the extracted value. You need to either unquote the extracted value e.g.
JSON_UNQUOTE(JSON_EXTRACT(request, "$.date_event"))
or using shorthand notation
request->>'$.date_event'
Or add the double quotes into the STR_TO_DATE template i.e.
STR_TO_DATE(JSON_EXTRACT(request, "$.date_event"),'"%d-%m-%Y %H:%i:%S"'))
Demo on dbfiddle

Neo4j Date conversion and comparison?

I was working with MS access database.MY datetime is now like "05-03-2016 14:55:20" .I need to convert it into datetime format in neo4j.1.How to do it ? 2.After conversion I need to use date filter i.e I want to find all nodes created between 2 dates.How to do it ?
If Neo4j is pulling data from Access query, construct field in query that calculates date to a string that Neo4j can then convert to date with its DateTime function.
Format([fieldname], "yyyy/mm/dd hh:nn:ss")
You can use the APOC function apoc.date.parse() to convert your MS Access datetime string into the number of seconds from the UNIX epoch, and then construct a neo4j datetime from that value. For example, this will return a neo4j datetime that represents your sample time string:
RETURN datetime({
epochSeconds: apoc.date.parse('05-03-2016 14:55:20', 's', 'MM-dd-yyyy HH:mm:ss')
})
A neo4j temporal type can only be compared directly to the same type. For instance, to compare a datetime X to a date Y, you can convert X to a date before doing the comparison. The following sample query will return true:
WITH
datetime({
epochSeconds: apoc.date.parse('05-03-2016 14:55:20', 's', 'MM-dd-yyyy HH:mm:ss')
}) AS X,
date('2016-05-04') AS Y
RETURN date({date: X}) <= Y

Ionic 2 JSON dates and moment.js trouble

I have a problem with dates manipulation with Ionic and moment.js.
I store some dates from a ion-datetime component :
{"year":2017,"month":7,"day":26,"hour":null,"minute":null,"second":null,"millisecond":null,"tzOffset":0}
And use moment.js to "humanize" date display :
let somedate = moment(some.date);
console.log('JSON date : ' + JSON.stringify(some.date) + ' resolved as : ' + somedate.format('DD/MM/YYYY'));
And got result :
Original JSON date : {"year":2017,"month":7,"day":26,"hour":null,"minute":null,"second":null,"millisecond":null,"tzOffset":0} resolved as : 26/08/2017
As you can consider, there's on month offset between original JSON date and moment display date...
What i'm missing ?
The Javascript month are in range of 0-11 i.e. January is 0, February is 1 and likewise, therefore it is moving you to the next month. Try subtracting 1 from month value, to get the correct month.
var some = {'date':{"year":2017,"month":6,"day":26,"hour":null,"minute":null,"second":null,"millisecond":null,"tzOffset":0} };
let somedate = moment(some.date);
console.log('JSON date : ' + JSON.stringify(some.date) + ' resolved as : ' + somedate.format('DD/MM/YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Finally, with your answers, i resolved the problem with a conversion method that take the original JSON date format and return a Javascript Date object, using TypeScript :
private _JSONDateToDate(jsonDate: any){
if(typeof jsonDate == 'object')
return new Date(jsonDate.year, (parseInt(jsonDate.month) - 1), jsonDate.day);
return jsonDate;
}
I substract 1 month of the "month" property in order to have a correct Date object... then, i can manipulate it with moment.js

Is it possible to query for datetime ranges with the Stripe API?

Using the Stripe API, I'd like to be able to query for date ranges or, failing that, dates that are greater than or less than some arbitrary date.
I know that I can query for something based on an exact date, e.g.,:
https://api.stripe.com/v1/events?created=1336503409
But I want something along the lines of...
# Search for events where `created` is greater than the epoch time: 1336503400
https://api.stripe.com/v1/events?created__gt=1336503400
Yes. From https://stripe.com/docs/api?lang=curl#list_events
created: A filter on the list based on the events created date. The value can be a string with an exact UTC timestamp, or it can be a dictionary with the following options:
gt (optional) Return values should have been created after this timestamp.
gte (optional) Return values should have been created after or equal to this timestamp.
lt (optional) Return values should have been created before this timestamp.
lte (optional) Return values should have been created before or equal to this timestamp.
So with curl, you can build a request like this:
curl "https://api.stripe.com/v1/events?created%5Blt%5D=1337923293" \
-u ${YOUR_API_KEY}:
The unescaped query parameter is created[lt]=1337923293.
If you're looking how to do so with the ruby client, here it is:
Stripe::Charge.all(limit: 100, 'created[lt]' => timestamps })
Along the same lines. You can achieve the same thing with the Python client like so:
import stripe
from datetime import datetime, timedelta
my_date = '1/31/2011'
my_timestamp = datetime.strptime(my_date, "%m/%d/%Y").timestamp()
stripe.Charge.all(created={'lt': my_timestamp})
For those of us using the Go Api, both formats below works for selecting a range:
params := &stripe.ChargeListParams{}
params.Filters.AddFilter("created", "lt", strconv.Itoa(1592217599))
params.Filters.AddFilter("created", "gt", strconv.Itoa(1591612124))
i := charge.List(params)
params := &stripe.ChargeListParams{}
params.Filters.AddFilter("created[lt]", "", strconv.Itoa(1592217599))
params.Filters.AddFilter("created[gt]", "", strconv.Itoa(1591612124))
i := charge.List(params)

How do you compare dates in a LINQ query?

I am tring to compare a date from a asp calendar control to a date in the table.... here's what i have... it doesn't like the == ?
var query = from details in db.TD_TravelCalendar_Details
where details.StartDate == calStartDate.SelectedDate
&& details.EndDate == calEndDate.SelectedDate
select details;
In order for your query to work both details.StartDate and calStartDate.SelectedDate must be typed as System.DateTime.
What error are you receiving? Most likely one of these properties is a string and will need to be parsed into a DateTime instance for comparison.
What is the Type of details.StartDate and details.EndDate? Is it String? Maybe that's the problem. If the database type is String you should Parse the Date String into a DateTime and compare it with the calendar's selected date then.