Problem in formatting date time in momentjs

Problem in formatting date time in momentjs

arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0

I'm making date time difference between a date time from database and current time but it return string like:
PT6H8M38S
-PT42M57S

my code:

ar tdy= new Date();
var dt= tdy.getFullYear()+'-'+(tdy.getMonth()+1)+'-'+tdy.getDate();
var time= tdy.getHours() + ":" + tdy.getMinutes() + ":" + tdy.getSeconds();
var dateTimeJs= dt+' '+time;

var dateDTD= 2020-01-02 08:38:52

moment.duration(moment(dateTimeJs).diff(moment(dateDTD)))

I know this is not for moment js but i want to know is there a feature in datatable ?

Replies

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    I'm not clear what the issue is, but would use columns.render to modify the cell data to look how you want it.

    Colin

  • arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0
    edited January 2020

    The issue is i want to make date time difference between stored datetime in database and time now, so when i do i get string PT6H8M38S

    PT means Pakistan Time here but i want it to be more human friendly and readable, i try to format

    moment.duration(moment(dateTimeJs).diff(moment(dateDTD))).format('YYYY-MM-DD HH:MM:SS');

    but it return invalid date

  • kthorngrenkthorngren Posts: 20,308Questions: 26Answers: 4,769

    Maybe you can provide a simple test case that we can look at and help you build the proper moment statement. Moment.js might not be interpreting the datetime correctly with moment(dateTimeJs) or moment(dateDTD). You may need to include the source datetime format as described here:
    https://momentjs.com/docs/#/parsing/string-format/

    Kevin

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    The issue is i want to make date time difference between stored datetime in database and time now, so when i do i get string PT6H8M38S

    This doesn't sound like a problem specific to DataTables, more like a Moment/Javascript issue - it would be worth asking on StackOverflow for advice there.

    Colin

  • arsalansiddiquiarsalansiddiqui Posts: 128Questions: 15Answers: 0

    Thanks for your suggestions, i'll look into it.

This discussion has been closed.