Jump to content

Seperate date and time in javascript


Denton

Recommended Posts

Hi,

I am trying to separate date and time of a record in javascript and display it in grid. Like this:
image.png.51018f28876458d9a3b7d7c7c7e47b7f.png

 

In UniDBGrid1 > ClientEvents > ExtEvents > reconfigure i did this:
 

function reconfigure(sender, store, columns, oldStore, oldColumns, eOpts)
{
   columns.forEach(function(col,index,cols){
      if (col.text === "Sched Date") {
         col.renderer = function(value,metaData,record){
         
            /*
              start_date and end_date are both date time.
               
            */
            var from = record.get(1);// start_date
            var to = record.get(2);// end_date
             
            return '<span>Sep 22 2022</span><br/>' + 
                   '<span style="font-size: 9px;color: #808080">09:00am - 01:00pm</span>';         
         };
      }
   });
}


How can i achieve the screenshot above? (or just separate date and time)

Thanks,

Regards

Link to comment
Share on other sites

Here is a sample javaScript function you can play with to achieve what you want. I created this quickly so am => pm conversion might need adjusting:

const dateToString = function (d){
    const toTwoDigits = function (d){
       d = d.toString()
       if(d.length < 2){
         d = '0' + d;
       }
      return d;
  }

  const months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
  let day = d.getDate();
  let month = d.getMonth();
  let yr = d.getFullYear();
  let returnDate = months[month]+' '+toTwoDigits(day)+' '+yr;

  let hrs = d.getHours();
  let mins = d.getMinutes();
  let ampm = 'am';
  if(hrs > 12){
      hrs -= 12;
      ampm = 'pm';
  }
  let returnTime = toTwoDigits(hrs)+':'+mins+ampm;
  return returnDate + ' - '+ returnTime;
}

You can test by pasting in Chrome console followed by:

let today = new Date()

console.log(dateToString(today))

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...