简体   繁体   中英

Javascript time and date change by timezone difference in hours

I have this code, which is to change the time and date - according to timezone difference for example 5 hours (indicated by /////TIME ZONE DIFFERECEN///// ), but it is not working how i would expect it to run; eg: not changing the date and time when applicable. Can anyone help.

 var dateObj = new Date(); var month = dateObj.getUTCMonth() + 1; //months from 1-12 var day = dateObj.getUTCDate(); var year = dateObj.getUTCFullYear(); var months = ["31"]; if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { months.push("29"); } else { months.push("28"); } monthsappend = ["31", "30", "31", "30", "31", "31", "30", "31", "20", "31"]; months.concat(monthsappend); time = "7:00pm"; var hours = Number(time.match(/^(\\d+)/)[1]); var minutes = Number(time.match(/:(\\d+)/)[1]); var AMPM = time.match(/\\s?([AaPp][Mm]?)$/)[1]; var pm = ['P', 'p', 'PM', 'pM', 'pm', 'Pm']; var am = ['A', 'a', 'AM', 'aM', 'am', 'Am']; if (pm.indexOf(AMPM) >= 0 && hours < 12) hours = hours + 12; if (am.indexOf(AMPM) >= 0 && hours == 12) hours = hours - 12; var sHours = hours.toString(); var sMinutes = minutes.toString(); if (hours < 10) sHours = "0" + sHours; if (minutes < 10) sMinutes = "0" + sMinutes; timearr = [sHours, sMinutes]; timearr[0] += 5; /////////////////////////////// TIME ZONE DIFFERENCE//////////////////////////// if (time.toLowerCase().includes("pm")) { } if (timearr < 0) { day -= 1; if (day == 0) { month -= 1; if (month == 0) { month = 12; year = dateObj.getFullYear() - 1; } day = months[month - 1]; } } else { if (timearr => 24) { timearr[0] = 24 - timearr[0]; if (day == months[month - 1]) { day = 1; month += 1; if (month == 12) { month = 1; year += 1; } } } } newdate = day + "/" + month + "/" + year; console.log(newdate); console.log(timearr[0]); 

Well, first I'll point out the bugs:

  • You have an arrow function => instead of a greater-than-or-equal-to operator >= (Thanks RobG).
  • You treat timearr as both an array, and later as a single number. I think you meant to compare timearr[0] .
  • When you add the 5 hours, you're concatenating strings. "19" + 5 === "195" . You need to work with numbers here, not strings.
  • You forgot to adjust timearr[0] in the first section of the large if statement.
  • You have subtraction in opposite order when you adjust timearr[0] in the second section of the large if . ( 24 - 26 === -2 , you probably meant 26 - 24 === 2 ).
  • At the end, you increment month += 1 before checking if (month == 12) . Either that needs to be in an else, or you'd have to check for month === 13 .
  • You're using == in places where === would be more appropriate.
  • You forgot var when you declared timearr .
  • You took the current date, but hard-coded the time.

Overall, it looks like you are trying to output the current date, in day/month/year format, at UTC+5. There are many simpler ways to do what you're asking. For example:

// Get the current moment in time, as a Date object.
var d = new Date();

// Add 5 hours of absolute duration.  The setter handles the bubbling for you.
// Be sure to use UTC here, to avoid interference from transitions of the local time zone.
d.setUTCHours(d.getUTCHours() + 5);

// Get the properties we want to display.
var year = d.getUTCFullYear();
var month = d.getUTCMonth() + 1;
var day = d.getUTCDate();

// Construct the string for output in the desired format.
var s = day + '/' + month + '/' + year;

Or with Moment.js :

var s = moment().utcOffset(5).format('D/M/YYYY');

Do keep in mind also "Time Zone != Offset". It just so happens that all the places in the world that currently use UTC+5 use it year-round (ex, Pakistan) but if you were talking about US Eastern Time, it would be UTC-5 for some parts of the year, and UTC-4 for other parts of the year.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM