2

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?

adnan khan
  • 81
  • 1
  • 6
  • Check if this stackoverflow answer helps you: https://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format/ – Drago96 May 10 '20 at 13:18
  • how implement in my code ? can write the code plz? [@](https://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format/) – adnan khan May 10 '20 at 13:57

2 Answers2

1

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;
}
Drago96
  • 741
  • 3
  • 13
  • `%` is actually a [multiplicative operator](http://ecma-international.org/ecma-262/10.0/#sec-multiplicative-operators) that returns the remainder, it doesn't conform to typical modulo behaviour, see [JavaScript % (modulo) gives a negative result for negative numbers](https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers). ;-) – RobG May 11 '20 at 01:49
  • Didn't knew that! Luckily in this situation it wouldn't change the result, but thank you, I'll keep that in mind for the future :) sometimes javascript's weird... – Drago96 May 11 '20 at 06:45
  • Yes, people don't often want modulo of negative numbers. Much of javascript's behaviour is copied from Java circa 1994 (i.e. when Java was also brand new) and has been in use for way too long to change fundamental stuff like `%` now. :-) – RobG May 11 '20 at 07:21
1

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()));
RobG
  • 124,520
  • 28
  • 153
  • 188