In my AppSync mapping request template, I need to add or subtract day from the current date.
All I could find are just formatting and parsing time helpers: Time Helpers in $util.time
#set( $todayString = $util.time.nowISO8601())
todayString is then like: 2019-08-23T09:00:00.000Z but I need to set new variables representing same time but one day prior current date or one day after it with the same formatting.
Is it possible at all using only vtl - mapping request template for my DynamoDB datasource?
I found a solution by using epoch time utils. Since it returns long, we can manipulate dates like in this sample and then convert it back to ISO8601 using existing time helper which accepts long value and returns formatted one further needed.
#set( $currentTimeEpoch = $util.time.nowEpochMilliSeconds())
#set( $fromStartEpoch = $currentTimeEpoch + (1000 * 60 * 60 * 24))
#set( $currentTime = $util.time.epochMilliSecondsToISO8601($currentTimeEpoch))
#set( $fromStart = $util.time.epochMilliSecondsToISO8601($fromStartEpoch))
Related
I have a command where I can enter a specific date for start date, please see below a part of the command I am using.
and when I am calling it in the test I need to enter a dynamic date like today <=(+30 days)
cy.create123((new Date().getDate() - 1), '2023-08-07')
ofc it did not work, but I have no idea how can I do it. How I can setup to cy.command to get always today-1 as startDate!
My issue is to make the dynamic enter date to work on Cypress.Commands()
TLDR
Install dayjs and use
const startDate = dayjs().add(-1, 'day').format('YYYY-MM-DD')
cy.create123(startDate, '2023-08-07')
The Custom Command and the cy.request() inside it are expecting the date as a string type.
Your calculated dynamic date (new Date().getDate() - 1) is giving you a number type.
But .toISOString() only works on Date types, not number types.
So after doing math on the Date(), you get a number which must be converted into a Date and then into a string.
const today = new Date()
const yesterday = new Date(today.setDate(today.getDate() -1))
const startDate = yesterday.toISOString()
But even that's not the end of the problems, because the timezone might give you invalid dates.
I recommend using dayjs as shown above.
You can do something like this. Instead of subtracting 1, I am decreasing the day with 24 hours.
cy.create123(new Date(Date.now() - (3600 * 1000 * 24)).getUTCDate(), '2023-08-07')
Considering today is 29 Aug, this will give the output as 28.
To get the date in the format yyyy-mm-dd use:
new Date(Date.now() - ( 3600 * 1000 * 24)).toISOString().slice(0, 10)
I am trying to convert a long utc value into "yyyy-MM-dd HH:mm:ss" formatted pattern. I am expecting my data to be converted on 24 hours range scale and in GMT. My code passes all the test cases, I push the data into database using the jar that is newly built with this code -
dbRecord("order_dt_utc") = if (orderTs.isDefined) Some(new DateTime(orderTs.get, DateTimeZone.UTC).toString("yyyy-MM-dd HH:mm:ss")) else None
and now, when I query my database, I find that the data is still converting on 12 hours range. The query -
SELECT order_id, order_dt, order_dt_utc, order_ts_utc, from_unixtime(order_ts_utc/1000) FROM order_items where order_dt >= '2018-08-01' AND order_dt <= '2018-08-02' ORDER BY order_dt_utc LIMIT 1000;
And you can see the the values are not matching in the columns from_unixtime(order_ts_utc/1000) and order_dt_utc -
I am not able to figure the reason for this behaviour.
To convert Time Zone use the function first:
CONVERT_TZ (dateobj, oldtz, newtz)
After that use the date_format function:
date_format(from_unixtime(order_ts_utc), '%Y-%m-%d %H:%i:%s');
to format your time to 00-23 format.
I creating an application, for that I need to find data by month using JPA and java.time.LocalDate. So, is it possible to retrieve data by month from mysql?
Thanks in advance for help.
First find start and end date of month and use between method of JPA to find data of current month.
LocalDate start = LocalDate.ofEpochDay(System.currentTimeMillis() / (24 * 60 * 60 * 1000) ).withDayOfMonth(1);
LocalDate end = LocalDate.ofEpochDay(System.currentTimeMillis() / (24 * 60 * 60 * 1000) ).plusMonths(1).withDayOfMonth(1).minusDays(1);
In Repository
List<Object> findByCreatedateGreaterThanAndCreatedateLessThan(LocalDate start,LocalDate end);
Its better to use the between keyword, it makes things allot shorter.
List<Object> findByCreatedateBetween(LocalDate start,LocalDate end);
Also if you want to use the LocalDate or LocalDateTime objects with Spring Data you should use the converter class Jsr310JpaConverters or else the documents will be stored as Blobs instead of Dates (which is bad for portability of the database). Please see this tutorial on how to implement the Converter.
https://www.mkyong.com/spring-boot/spring-boot-spring-data-jpa-java-8-date-and-time-jsr310/
tl;dr
YearMonth.now( ZoneId.of( "Pacific/Auckland" ) ) // Get current month for particular time zone.
.atDayOfMonth( 1 ) // Get the first date of that month.
.plusMonths( 1 ) // Get first of next month for Half-Open query.
Details
Assuming your column in MySQL is of DATE type…
LocalDate
The LocalDate class represents a date-only value without time-of-day and without time zone.
Time zone
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
YearMonth
The YearMonth class represents an entire month. Getting the current month requires a time zone as discussed above. Around the beginning/ending of the month, the current moment could be “next” month in Auckland New Zealand while still “previous” month in Kolkata India.
YearMonth currentMonth = YearMonth.now( z ) ;
Get the first date of the month.
LocalDate start = currentMonth.atDayOfMonth( 1 ) ;
Half-Open
Generally best to use the Half-Open [) approach to defining a span of time, where the beginning is inclusive while the ending is exclusive. So defining a month means starting with the first date of the month and running up to, but not including, the first date of the following month.
LocalDate stop = start.plusMonths( 1 ) ;
Query
Do not use the BETWEEN command in SQL as it is fully closed [], both beginning and ending being inclusive. Half-Open uses >= & < logic.
SELECT when FROM tbl
WHERE when >= start
AND when < stop
;
it's also useful
#Query("from PogWorkTime p where p.codePto = :codePto and month(p.dateApply) = :month and year(p.dateApply) = :year")
Iterable<PtoExceptWorkTime> findByCodePtoAndDateApply_MonthAndDateApply_Year(#Param("codePto") String codePto,#Param("month") int month, #Param("year") int year);
I am new to Appsript Script.Db
We have a spreadsheet whit about 300 row's of date in the first column date : 3-1-2014 5:50:46.
When we do a function loadDatabaseFromSheet() we get the error date Timestamp is not a number.
ScriptDB cannot store Date objects directly; instead, you must store a representation of the >date and reconstruct it later. If you don't intend to search based on dates, then you can >store the numeric timestamp from the Date object like this:
var date = new Date('1/1/2014');
var item = {
timestamp: date.getTime();
}
var record = db.save(item);
Do we have change all the date by hand in a proper way from 3-1-2014 5:50:46 to '3/1/2014'?
Hope that there is a better way to get this done?
// How and where can i make a representation: ('1/1/2014'); I hope i don't have to change
// all the date's in the spreadsheet by hand ?My spreadsheet has 300 Row's, first collumn =
The script DB cannot save Date datatype by default, so what you need to do is covert it to its time representation and save it to DB as the above info suggests.
As for your question regarding the need to change date format, it will not be necessary just do a (new Date(value)).getTime() at the time of saving to the DB. Where value represents the datetime in the first column.
I am creating project in yii. I am having Poll table with the fields as-
-pollId
-pollQustion
-isActive
-publishDate
I want to check weather publishdate of poll is not greater than current date.
I am implementing as-
$CurrrentDate=new CDbExpression('NOW()');
if($record->publishDate < $CurrrentDate))
{
some code......
}
But its not executing correctly. Code is executing even if publish date is greater than current date. So how to make this comparison in yii framework. please help me
PHP do not have a date format. When you compare $CurrentDate and $record->publishDate you compare strings. Well, for right compare you need to convert it to Unix's timestamp
// here you set right date/time pattern
// You need to explain CDateTimeParser for your pattern
// just easy use `strtotime` if you have 2012-12-12 12:12:12
$publishTimestamp = strtotime($record->publishDate);
// $publishTimestamp = CDateTimeParser::parse($record->publishDate, 'yyyy-MM-dd hh:mm:ss');
// if you need to use Hours and minutes just easy use `time()`
$currentDateTimestamp = time();
// $currentDateTimestamp = strtotime(date('Y-m-d H:i:s'));
// now we can compare
if ($publishTimestamp < $currentDateTimestamp) {
// do some
}