[英]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; 我有这段代码,它将更改时间和日期-根据时区差异,例如
5
小时(由/////TIME ZONE DIFFERECEN/////
表示),但是它不起作用,我怎么期望跑步; 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: 好吧,首先我要指出这些错误:
=>
instead of a greater-than-or-equal-to operator >=
(Thanks RobG). =>
而不是大于或等于运算符>=
(感谢RobG)。 timearr
as both an array, and later as a single number. timearr
既视为数组,又将其视为单个数字。 I think you meant to compare timearr[0]
. timearr[0]
。 "19" + 5 === "195"
. "19" + 5 === "195"
。 You need to work with numbers here, not strings. timearr[0]
in the first section of the large if
statement. if
语句的第一部分中调整timearr[0]
。 timearr[0]
in the second section of the large if
. if
的第二部分中调整timearr[0]
时,减法顺序相反。 ( 24 - 26 === -2
, you probably meant 26 - 24 === 2
). 24 - 26 === -2
,您可能表示26 - 24 === 2
)。 month += 1
before checking if (month == 12)
. if (month == 12)
之前增加month += 1
。 Either that needs to be in an else, or you'd have to check for month === 13
. month === 13
。 ==
in places where ===
would be more appropriate. ===
更合适的地方使用==
。 var
when you declared timearr
. timearr
时,您忘记了var
。 Overall, it looks like you are trying to output the current date, in day/month/year format, at UTC+5. 总体而言,您似乎正在尝试以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 : 或使用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. 碰巧的是,目前世界上所有使用
UTC+5
地方都全年使用它(例如,巴基斯坦),但是如果您谈论的是美国东部时间,那么一年中的某些时间段将是UTC-5
,以及UTC-4
(今年其他时间)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.