-1

I am trying to pass a time stamp to my API that comes in the format of YYYY-MM-DD HH:MM:SS but i need to manipulate the time to add 5 hours.

Have I done something wrong here? Do I need to convert it to a JavaScript date first?

var manDate = "2020-08-16 16:15:00"
manDate.setHours(manDate.getHours() + 5);
data.manDate = manDate
console.log(manDate)

Expected output - 2020-08-16 21:15:00

VLAZ
  • 22,934
  • 9
  • 44
  • 60
luke jon
  • 89
  • 1
  • 7

3 Answers3

2

When you create a var for date, you need to add the 'new Date()' method.

var manDate = new Date("2020-08-16 16:15:00");
manDate.setHours(manDate.getHours() + 5);
console.log(manDate.getHours());

And to log the hours use getHour() again in the log statement.

1

The question was asked to give a solution in java earlier. Below is the answer as per java.

Date newDate = DateUtils.addHours(oldDate, 5);
Lahiru Wijesekara
  • 603
  • 1
  • 9
  • 21
1

Use simpleDateFormat to format the date, then cast the formatted date to calendar and add hours to it.

Try with below code.

SimpleDateFormat sdfObj = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
sdfObj.parse("2020-08-16 16:15:00");                   
Calendar calendar = sdfObj.getCalendar();
calendar.add(Calendar.HOUR_OF_DAY, 5);