Modx Evo Date Difference Snippet - code-snippets

Using Modx evo, I am trying to use the following snippet to display the date difference between published date and current date (in days), but getting weird output. What am I doing wrong?
<?php
$date2=$modx->documentObject['createdon'];
$date1=time();
$dateDiff = $date1 - $date2;
$daysOld = floor($dateDiff/(60*60*24));
return $daysOld;
?>

The thing is that the date is stored as a SQL-date, not a timestamp.
Read the docs: http://rtfm.modx.com/display/revolution20/Date+Formats
So, this should work:
$dateDiff = $time() - strtotime($modx->documentObject['createdon']);
$daysOld = floor($dateDiff/(60*60*24));
return $daysOld;

Related

How do you convert a date in XSLT2.0?

How can I format the resulted date below to display like this: MM/dd/YYYY (e.g.: 05/04/2020)?
The $getField will be: 2020-05-04T15:00:12
My value in the template is:
<xsl:value-of select="xs:dateTime($getField)+xs:dayTimeDuration('PT1H')"/>
Currently, it displays as follows: 2020-04-05T16:00:12
Each time I try using format-date, I get an error.
Also, if I will use xs:date(xs:dateTime($getField)+xs:dayTimeDuration('PT12H')), it will display only the date, without hours/minutes, but still not sure how can I change the order for date?
Thanks!
Use the format-dateTime() function with the desired picture "[M01]/[D01]/[Y0001]":
format-dateTime(xs:dateTime($getField)+xs:dayTimeDuration('PT1H'), "[M01]/[D01]/[Y0001]")

Date.strptime throwing invalid date for "%Y%W" format

My date = 1st January, 2015.
I did
date_formatted = date.strftime('%Y%W') which returned "201500"
Then when am trying to do Date.strptime(date_formatted, '%Y%W') it's throwing an invalid date error even though the original string was returned from strptime's counterpart srftime.
Found the solution. Time.strptime(date_formatted, '%Y%W') is what will work here.

af:inputDate is returning GMT instead of user selected date in ADF

I am using af:inputDate tag to capture the date in adf.
My web page looks like -
The date gets stored at startDate (which is java.util.Date).
Now after
DataObjectEncodingUtils.encodeDate(startDate)
it is returning 2014-02-01 18:30:00 which is exactly 5.30 hours behind the selected date.
Here is the entry details in my trinidad-config.xml -
<?xml version="1.0" encoding="windows-1252"?>
<trinidad-config xmlns="http://myfaces.apache.org/trinidad/config">
<skin-family>mySkin</skin-family>
<formatting-locale>en_GB</formatting-locale>
<time-zone>Asia/Calcutta</time-zone>
<!--<time-zone>IST</time-zone>-->
</trinidad-config>
Am i missing something ?
-Amit
Have you tried using an component together with the inputDate?
You can set the date format and the timezone in which you want the date displayed.

Format datetime in where condition in EasyQuery

Mine is a MVC application having easyquery widget.
I need to format datetime columns in where condition like '2013-05-06 12:20:35'.
But when I tried
dbQuery.Formats.DateTimeFormat = "'yyyy-MM-dd HH:mm:ss'";
It is simply omitting single quotes and giving me like 2013-05-06 12:20:35.
If I am replacing single quotes with # like
dbQuery.Formats.DateTimeFormat = "#yyyy-MM-dd HH:mm:ss#";
It is getting reflected like #2013-05-06 12:20:35#.
How to format this?
query.Formats.DateTimeFormat = "yyyy-MM-dd hh:mm:ss";
query.Formats.QuoteTime = True;
Found from here

Saving and Querying a Date in GORM Grails

I have a database table TableA, which has a column 'theDate' for which the datatype in the database is DATE.
When I save a java.util.Date to 'theDate' through GORM it appears to save just the date value when I look at the data in the table by just executing select * from TableA.
However, when I run a query such as:
select * from TableA where theDate = :myDate
No results are found, but if I run something like;
select * from TableA where theDate <= :myDate
I do get results.
So it's like the Time is relevant.
My question is how do I save a Date and query for a Date ignoring the Time completely and just matching on an exact Date only?
Thanks.
note: I have also tried using sql.Date and util.Calendar but to no success.
clearTime()
You can use clearTime() before saving and before comparing to zero out the time fields:
// zero the time when saving
new MyDomain(theDate: new Date().clearTime()).save()
// zero the target time before comparing
def now = new Date().clearTime()
MyDomain.findAll('SELECT * FROM MyDomain WHERE theDate = :myDate', [myDate: now])
joda-time plugin
An alternative would be to install the joda-time plugin and use the LocalDate type (which only holds date information, no times) instead of Date. For what it's worth, I don't think I've worked on a project with dates without using the Joda plugin. It's completely worth it.
If you have date saved without clearing you could retrieve it using range, as Jordan H. wrote but in more simple way.
def getResults(Date date) {
def from = date.clearTime()
def to = from + 1
def results = MyDomain.findAll("from MyDomain where dateCreated between :start and :stop" ,[start:from,stop:to])
}
Your question may be a duplicate. See Convert datetime in to date. But if anyone has more recent information, that would be great.
If that doesn't help, you can hack it the way I might, with a BETWEEN restriction, e.g.
def today = new Date()
def ymdFmt = new java.text.SimpleDateFormat("yyyy-MM-dd")
def dateYmd = ymdFmt.format(today)
def dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
def startDate = dateTimeFormat.parse("${dateYmd} 00:00:00");
def endDate = dateTimeFormat.parse("${dateYmd} 23:59:59");
MyDomain.findAll("from MyDomain where dateCreated between ? and ?", [startDate, endDate])
It's definitely not pretty, but it may get you where you're going.
I figured it out.
I used DateGroovyMethods.clearTime to clear the time value before saving.
You can use the DB type date not datetime , in the filed type

Resources