Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Convert UNIX timestamp to date using Vue JS

Avatar

Level 5

I am getting created date of the pages in aem using query builder through a servlet. But the output date is in UNIX format. I want to render the date in correct date format like Jan 14, 2022. Is there a way I can change this using VueJS. Or any other to do this?

 

5 Replies

Avatar

Community Advisor

hi @nikita24tailor 

You may have to write custom java logic to render the date format as per your requirement :

 

Similar discussion : https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-query-debugger-date-fo...

Avatar

Level 5

I tried doing that using the following code:

String text = props.get("jcr:created").toString();
long timeStamp =  Long.parseLong(text);
        
//convert seconds to milliseconds
Date date = new Date(timeStamp); 
// format of the date
SimpleDateFormat jdf = new SimpleDateFormat("dd MMM yyyy");
String java_date = jdf.format(date);
System.out.println("Created Date: "+java_date);

But the problem is jcr:created returns an object which has the following values:
java.util.GregorianCalendar[time=1654598024706,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=5,WEEK_OF_YEAR=24,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=158,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=3,SECOND=44,MILLISECOND=706,ZONE_OFFSET=19800000,DST_OFFSET=0]
But I only want the timestamp. Is there a way that I can get only the timestamp from jcr:created?

Avatar

Community Advisor
import moment from 'moment';

Vue.filter('formatDate', function(value) {
    if (value) {
        return moment(String(value)).format('MM/DD/YYYY hh:mm')
    }
});

Here is the sample code, you can use for converting to a different format.

Avatar

Level 5

The value of the date is returned through a servlet as json data. How can I use this code for the same?

Avatar

Community Advisor

Hi @nikita24tailor ,

Try this statement

vueJSdate = new Date(unixTimestamp * 1000);

Timestamps of UNIX will be in seconds and Date() required in milliseconds.

Found similar issue here: https://stackoverflow.com/questions/72643134/convert-unix-timestamp-to-date-using-vuejs

Hope that helps!

Regards,

Santosh