I am trying to run a query at the repository level of my nestjs application.
The date format of the column (runDateTime) in my DB is isoString. I want to find all the records that have runDateTime of today's date.
I have a function called Parsebackenddate that essentially converts iso string to YYYY-MM-DD. how do i use this function with "schedule.runDateTime" in order to compare both dates in the format YYYY-MM-DD?
Or is there an alternative?
if (getToday) {
const todayDate = dayjs().format('YYYY-MM-DD');
query.where('schedule.runDateTime = :todayDate', {
todayDate,
});
``
Much appreciated.
first, you have to get a date from the database and hold it in a class variable then compare.
something like that may this could help.
var _orderBean = new date_beans();
_orderBean.dueDate = _response[a].dueDate.toISOString().slice(0,10); //YYYY-MM-DD
then compare
var d = new Date(); //todaydate
if(d.toISOString().slice(0,10)>_orderBean.dueDate)
// TodayDate > DueDate
Related
I am wanting to display in a react native app a date given by the API.
The API is returning the following value:
"2022-06-20T14:00:00.000"
I need to display to the user
2:00 PM
The problem is that when I try this:
const dateString = "2022-06-20T14:00:00.000"
const date = new Date(dateString);
console.log(date)
The date has been changed to:
2022-06-20T14:00:00.000Z
Meaning it is 2pm UTC time when it should be 2pm local time.
On the end of the new Date variable, if you add the .toLocaleTimeString(), it should then output as 2:00:00PM.
const date = new Date(dateString).toLocaleTimeString();
you can use moment library with its localized formats,
here is the documentation for more of its formats https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/
const dateString = "2022-06-20T14:00:00.000"
console.log(moment(dateString).format('LT'))
I've been building some simple linear regression models in Tensorflow.js with various types of data sets. However, I would now like to see what the relationship is between dates and price in my dataset.
In previous models, I normalise the price or other features so that the tensor is expressed as a vector between 0 and 1.
How would one do this dates, where the first date would have to be 0 and the last date in the range 1? Furthermore I would need to denormalise the Tensor afterwards.
I can convert the date into a unix timestamp using a library like date.fns... but I wondered if there might be a cleaner way to do this.
My normalise and denormalise functions :
function normalise (tensor) {
const min = tensor.min();
const max = tensor.max();
const normalisedTensor = tensor.sub(min).div(max.sub(min))
return {
tensor : normalisedTensor,
min,
max
}
}
function denormalise(tensor, min, max) {
const denormalisedTensor = tensor.mul(max.sub(min)).add(min);
return denormalisedTensor
}
The basic principle is that you need to convert the date formats (like 2021-03-21) to numbers, then you can use the normalise and denormalise functions that you already have. In your solution you have done that, however, you can write it in a simpler way. You can use date-fns, but the standard JavaScript Date object is also sufficient so I will use that.
Assuming your input is an array of date strings like 2021-05-01, then you can do this:
const dateStrings = ['2021-05-01', '2021-05-07', '2021-05-31'];
const timestamps = dateStrings.map(dateString => new Date(dateString).valueOf());
const unnormalisedTensor = tf.tensor1d(timestamps);
Then you can use the functions you have for normalisation (and denormalisation).
You can convert back from timestamps to date strings like this:
const timestamps = await unnormalisedTensor.array();
const dateStrings = timestamps.map(timestamp => new Date(timestamp).toISOString().substring(0,10));
I achieved this by using date.fns to convert the dates into timestamps and then normalising with the following function
const getTimestamps = (data) => {
data.forEach(record => {
let newDate = record.time
newDate = new Date(newDate);
const timestamp = getUnixTime(newDate);
console.log(newDate, timestamp)
record.timestamp = timestamp
}
I'm trying to compare the date of videos on a webpage to today's date. If the difference between the two dates is more than X days, report back as false.
The videos on the webpage have a tag in them which uses the format yyyy-mm-dd
I've got a selector set up to find the videos const videoDate = Selector('OPTA-video').withAttribute('data-secondary-time')
Now how do I set a variable to today's date and compare the two? I'm completely stuck!
I was using Katalon Studio before and here's the groovy script that did the same job:
String videoDate = WebUI.getAttribute(findTestObject('OPTA-video'), 'data-secondary_time')
LocalDate todaysDate = LocalDate.now()
LocalDate videoDateParsed = LocalDate.parse(videoDate, dtf)
if (ChronoUnit.DAYS.between(videoDateParsed, todaysDate) > 1) {
KeywordUtil.markFailed('The videos are 2+ days old.')
} else {
KeywordUtil.logInfo('The videos are up to date.')
}
You can use the getAttribute TestCafe method to access an attribute value. Then, parse the attribute value into the JavaScript Date object:
String videoDate = Selector('OPTA-video').getAttribute('data-secondary-time');
Date videoDateParsed = Date.parse(videoDate);
Date todaysDate = Date.now()
...
In the following thread you can find how to compare Date objects.
This is one of the scripts that I am using.
//getting your XPath test value into a string
String ann_time =
WebUI.getText(findTestObject("ObjectRepository/navigateTOElement/announcements_date"))
//converting time to simple date format
SimpleDateFormat sdf = new SimpleDateFormat('HH:mm')
Date sdf_anntime = sdf.parse(new String(ann_time))
//getting Current time
SimpleDateFormat dateFormatGmt = new SimpleDateFormat('HH:mm')
dateFormatGmt.setTimeZone(TimeZone.getTimeZone('GMT'))
SimpleDateFormat dateFormatLocal = new SimpleDateFormat('HH:mm')
currDate = dateFormatLocal.parse(dateFormatGmt.format(new Date()))
// time gap in long format
long duration = currDate.getTime() - sdf_anntime.getTime()
//time gap to mins
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration)
//compare time gap with globale variable
if (diffInMinutes < GlobalVariable.News_updated_time) {
log.logInfo("system is getting updated,last updated "+ diffInMinutes + "min ago")
} else {
CustomKeywords.'errorMessage.logFailed.markStepFailed'('from 1 h, system was not updated')
log.logInfo('from '+ diffInMinutes+ 'h, system was not updated')
}
I am using ChannelSftp(jsch-0.1.42.jar) to copy files from a unix server and can successfully do it but the last modified date gets overwritten by the current time.
I am using
chanSftp.get(nextName, "C:/Test/" + nextName);
Is there any way of retaining the last modified time of the remote file on the local copy?
I have the last modified time of the remote file which i got using
attrs = lsEntry.getAttrs();
Date modDate = (Date) format.parse(attrs.getMtimeString());
Using a SimpleDateFormat is of less performance. Instead one could use the methods getMTime() and getATime() directly. But they deliver a value reduced by the milliseconds.
That's why they return an int and not a long as expected in conformance to date.getTime().
SftpATTRS attrs = lsEntry.getAttrs();
Date dateModify = new Date(attrs.getMTime() * 1000L);
Date dateAccess = new Date(attrs.getATime() * 1000L);
In jsch-0.1.50 be careful of using getAtimeString() there is the factor 1000L missing.
In jsch-0.1.51 the getAtimeString() bug with the missing factor is fixed.
This changes the last modified time of the file downloaded from the remote server,
String remoteFilePath = "testDir/testFile.txt";
SftpATTRS attrs = sftpChannel.lstat(remoteFilePath);
SimpleDateFormat format = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date modDate = (Date) format.parse(attrs.getMtimeString());
String localFilePath = "C:/temp/downloadedFile.txt";
sftpChannel.get(remoteFilePath, localFilePath);
File downloadedFile = new File(localFilePath);
downloadedFile.setLastModified(modDate.getTime());
Merging the answers above to a working solution:
sftpChannel.get(REMOTE_FILE, LOCAL_FILE);
SftpATTRS attrs = sftpChannel.lstat(REMOTE_FILE);
Date dateModify = new Date(attrs.getMTime() * 1000L);
File downloadedFile = new File(LOCAL_FILE);
downloadedFile.setLastModified(dateModify.getTime())
i have a remote sql satabase, and i connect him by php with JSON.
I can get string and int data OK, but now i have a problem.... i have a datetime field on my SQL database, and i dont know how to get it with JSON into java.util.Date
this is the code i tryed, but it fails getting the Date correctly, it gives me an exception (java.lang.ClassCastException: java.lang.String)
code:
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
positions.add(new Position(json_data.getString("latitude"), json_data.getString("longitude"), (Date)json_data.get("timestamp")));
}
You will have to fetch the string sent in the JSON data and parse that string with something like SimpleDateFormat.
In my projects I have used two different solutions.
Storing the Date not as Date but as long(milisecs since 1.1.1980). In Java you can get this with date.getTime(). There should also a methode in PHP. The get the Date Object in Java just pass the long value to the constructor(date= new Date(long_value)).
With this Method you may have problems when dates comes from different time zones.
Write the Date as a formatted Date String in the JSON. How you encode the Date is up to you. A short sample give below. see[1] for further infos.
To get the Date you need a SimpleDateFormater.
DateFormat df = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
try
{
Date today = df.parse("2001.07.04 AD at 12:08:56 PDT");
System.out.println("Today = " + df.format(today));
}
catch (ParseException e)
{
e.printStackTrace();
}
[1]http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html