javascript dates with same date format

Viewed 91

I am facing an issue in javascript dates . i want to do same date format both previous date and start date

Excepted Output previous date

after 12AM shows 00:00:00 to 12:00:00

after 12PM shows 00:00:00 to 12:00:00

format

previous date 2020-05-10 16:31:28

start date

2020-05-10 02:00:00              //sample data
2020-05-10 05:00:00

My code:


var currentdate = new Date();
var prevdate = new Date(); //previous date

prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000));

var newprevious = GetFormattedDate(prevdate);
console.log(newprevious);

//Date format yyyy-mm-dd h:MM:ss
function GetFormattedDate(date) {
  var month = ("0" + (date.getMonth() + 1)).slice(-2);
  var day = ("0" + (date.getDate())).slice(-2);
  var year = date.getFullYear();
  var hour = ("0" + (date.getHours())).slice(-2);
  var min = ("0" + (date.getMinutes())).slice(-2);
  var seg = ("0" + (date.getSeconds())).slice(-2);
  return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + seg;
}

Note: i can't change the start_date it is fixed format

what should i change ? Anyone help me?

2 Answers

This really is a date formatting issue so probably a duplicate of How to format a JavaScript date.

As Drago96 has answered, you can use the remainder operator % to modify the hours value and add am or pm as appropriate

An alternative to using get methods and processing each part is to use the formatting options of Intl.DateTimeFormat object and formatToParts, e.g.

function formatDate(date) {
  let parts = new Intl.DateTimeFormat('default',{
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: true
  }).formatToParts(date).reduce((acc, part) => {
    if (part.type != 'literal') {
      acc[part.type] = part.value;
    }
    return acc;
  }, Object.create(null));
  
  return `${parts.year}-${parts.month}-${parts.day}\
 ${parts.hour}:${parts.minute}:${parts.minute}\
 ${parts.dayPeriod}`;
}

console.log(formatDate(new Date()));

If the problem is just that the hour is not in am/pm format, you can modify your function this way, using the modulo operator (I've also added am/pm at the end of the string):

var currentdate = new Date();
var prevdate = new Date(); //previous date

prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000));

var newprevious = GetFormattedDate(prevdate);
console.log(newprevious);

//Date format yyyy-mm-dd h:MM:ss
function GetFormattedDate(date) {
  var month = ("0" + (date.getMonth() + 1)).slice(-2);
  var day = ("0" + (date.getDate())).slice(-2);
  var year = date.getFullYear();
  
  // EDIT:
  var hour = date.getHours()%12; // using the modulo operator, you convert the hours to a range between 0 and 12

  if (hour == 0) {
      hour = 12;
  }
  
  var timeOfDay = "am";      

  if (date.getHours() >= 12) {
      timeOfDay = "pm";
  }

  hour = ("0" + hour).slice(-2);
  
 // END EDIT

  var min = ("0" + (date.getMinutes())).slice(-2);
  var seg = ("0" + (date.getSeconds())).slice(-2);
  return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + seg + " " + timeOfDay;
}

Related