best way insert time with zone JavaScript postgres 9.6 - postgresql

What is the best way to insert the now() date time in postgresql using JavaScript? I've followed some answers but nothing clear, I'd like to find pure JavaScript method, and not with moment.js

It would be so much better if you inserted the table's create SQL code, probably part of your application's code in javascript and more information about the situation you're in to your question, in order for people who want to help, to better understand it.
Assuming you have a table with a createdAt column which its type is TIMESTAMP and you want to fill the query below with proper value for :nowValue which is current date and time, and you don't want to use Postgres's built in functionalities (like CURRENT_TIMESTAMP) or any other javascript library (like moment):
INSERT INTO "myTable" ("createdAt") VALUES (:nowValue)
First you have to init a new Date object:
const nowValue = new Date();
Then you can convert the newly created object to string and use it in your query using different methods which return different values:
nowValue.toDateString(); // "Fri Jan 12 2018"
nowValue.toGMTString(); // "Fri, 12 Jan 2018 11:09:43 GMT"
nowValue.toISOString(); // "2018-01-12T11:09:43.153Z"
nowValue.toLocaleDateString(); // "1/12/2018"
nowValue.toLocaleString(); // "1/12/2018, 2:39:43 PM"
nowValue.toUTCString(); // "Fri, 12 Jan 2018 11:09:43 GMT"
Some of the above methods only outputs the date and not the time, which will make column's time value to be set to 00:00:00.000000.
For more detailed information about these methods and how they are different see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Related

Unable to sort datetime using variable computed from datetime string

I am trying to sort a table by datetime in descending order. I currently am trying to use a variable to be the field to be used in sorting.
Example data:
header A
datetime
First
01 Jan 2021
Second
17 Aug 2021
I created a variable, as seen below,to try to sort the records.
After creating the Long variable from the DATEVALUE expression, after converting to string, it shows null when I load it into preview. Also, it does not sort properly after using it as a sort field.
Some help is much appreciated.
The dataset included the epoch of the date. I used that to sort.

Hive datatype confusion

I have a large data and in that one field be like Wed Sep 15 19:17:44 +0100 2010 and I need to insert that field in Hive.
I am getting troubled for choosing data type. I tried both timestamp and date but getting null values when loading from CSV file.
The data type is a String as it is text. If you want to convert it, I would suggest a TIMESTAMP. However you will need to do this conversion yourself while loading the data or (even better) afterwards.
To convert to a timestamp, you can use the following syntax:
CAST(FROM_UNIXTIME(UNIX_TIMESTAMP(<date_column>,'FORMAT')) as TIMESTAMP)
Your format seems complex though. My suggestion is to load it as a string and then just do a simple query on the first record until you get it working.
SELECT your_column as string_representation,
CAST(FROM_UNIXTIME(UNIX_TIMESTAMP(<date_column>,'FORMAT')) as TIMESTAMP) as timestamp_representation
FROM your_table
LIMIT 1
You can find more information on the format here: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
My advice would be to concat some substrings first and try to convert only the day, month, year part before you look at time and timezone et cetera.

Saving date in sails js model

I am using moment as a third part library.
Every time a record is created in the model the date is stored with 0:00 time. Such as Thu Dec 03 2015 00:00:00 GMT+0200 (EET) .
var dateToday = moment(moment().format("YYYY-MM-DD HH:mm")).toISOString();
This is the date I am putting in the date field.
I've also tried with toDate() and new Date()... the same.
When I try to print it, the time is OK, but when I put it in a record it is always stored as 0:00.
What is your Database adapter? You DB field could be set to a simple date format?
Otherwise you should update your question with you DB Adapter and your Model definition (and if using SQL database, your scheme for that field)
You should be using datetime as your attribute values.
const moment = require('moment');
Here is a working example on how to save date (now) into MYSQL DB through ORM of Sails.js:
moment().format('YYYY-MM-DD HH:mm:ss')}
With the ORM class:
await TagsToScrape.update({
id: hashTag.id
}).set({lastProcessStarted: moment().format('YYYY-MM-DD HH:mm:ss')});
P.S. You can always check whether time is valid like this:
moment().format('YYYY-MM-DD HH:mm:ss')}.isValid() // true

Xpath current date

I have a webpage where a row displays name, relation and date. I want to search for the matching row as per my values. Using Xpath, I have built the below mentioned code. The only problem is the last part (the date). I want to pick up the current date and fit it into the search query..i.e. instead of 30 Sep 2013, I want it to search for 01 Oct 2013 (assuming today is this date).
Any clue how can i do that??
$x('//tr[descendant::b[text()="text1"] and descendant::a[#class="bill" and text()="for Automation"] and descendant::td[text()="30 Sep 2013"]]')
You have to build the expression dynamically and append the date string at the end based on some date object. The specific implementation depends on which programming language you're using

Filtering table rows with higher date than provided

I'm currently trying to do it that way:
// Creating date object
$date = new Zend_Date();
// Adding to it 4 weeks
$date->add('4', Zend_Date::WEEK); // it's expire day
// Getting date in integer(i guess it's unix timestamp yes?)
$date->get();
// Saving it to Mysql in field 'expire' with type Varchar
Then, when needed to get rows, that have date bigger(that haven't yet expired), than current I just add to SQL a simple statement WHERE expire >= $current_date.
Or there is better way to do it? Or how it happens usually?
I would recommend using the native MySQL DATETIME column in your table. This is how you'd retrieve the date for MySQL:
$date->get('yyyy-MM-dd HH:mm:ss');