Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript时间和日期随时区差异的变化(以小时为单位)_Javascript_Date_Time_Timezone_Timezone Offset - Fatal编程技术网

Javascript时间和日期随时区差异的变化(以小时为单位)

Javascript时间和日期随时区差异的变化(以小时为单位),javascript,date,time,timezone,timezone-offset,Javascript,Date,Time,Timezone,Timezone Offset,我有这个代码,它可以根据时区的不同来更改时间和日期,例如5hours(由/////timezone differencen///////code>表示),但它不能按我预期的方式运行;e、 g:适用时不更改日期和时间。有人能帮忙吗 var dateObj=new Date(); var month=dateObj.getUTCMonth()+1//1-12个月 var day=dateObj.getUTCDate(); var year=dateObj.getUTCFullYear(); 风险值

我有这个代码,它可以根据时区的不同来更改时间和日期,例如
5
hours(由
/////timezone differencen///////code>表示),但它不能按我预期的方式运行;e、 g:适用时不更改日期和时间。有人能帮忙吗

var dateObj=new Date();
var month=dateObj.getUTCMonth()+1//1-12个月
var day=dateObj.getUTCDate();
var year=dateObj.getUTCFullYear();
风险值月数=[“31”];
如果((第%4年==0)和&(第%100年!=0))| |(第%400年==0)){
月。推送(“29”);
}否则{
月份。推送(“28”);
}
monthsappend=[“31”、“30”、“31”、“30”、“31”、“31”、“30”、“31”、“20”、“31”];
月份。concat(monthsappend);
time=“7:00pm”;
var小时数=数量(time.match(/^(\d+/)[1]);
var minutes=Number(time.match(/:(\d+/)[1]);
var AMPM=时间匹配(/\s?([AaPp][Mm]?)$/)[1];
var pm=['P','P','pm','pm','pm','pm'];
var am=['A','A','am','am','am','am'];
如果(pm.indexOf(AMPM)>=0&&hours<12)小时=hours+12;
如果(am.indexOf(AMPM)>=0&&hours==12)小时数=hours-12;
var sHours=hours.toString();
var sMinutes=minutes.toString();
如果(小时<10)寿=0“+寿;
如果(分钟<10)sMinutes=“0”+sMinutes;
timearr=[叫喊,微笑];
timearr[0]+=5;
///////////////////////////////时区差////////////////////////////
if(time.toLowerCase()包括(“pm”)){
}
if(timearr<0){
第-=1天;
如果(天==0){
月份-=1;
如果(月份==0){
月=12;
year=dateObj.getFullYear()-1;
}
天=月[月-1];
}
}否则{
如果(timearr=>24){
timearr[0]=24-timearr[0];
如果(天=月[月-1]){
日=1;
月份+=1;
如果(月份==12){
月=1;
年份+=1;
}
}
}
}
新日期=日+“/”+月+“/”+年;
console.log(newdate);

console.log(timearr[0])首先,我要指出错误:

  • 您有一个箭头函数
    =>
    ,而不是大于或等于运算符
    =
    (感谢RobG)
  • 您将
    timearr
    视为一个数组,稍后将其视为一个数字。我想你的意思是比较
    timearr[0]
  • 当您添加5小时时,您正在连接字符串<代码>“19”+5==“195”
。你需要在这里使用数字,而不是字符串
  • 您忘记在大型
    if
    语句的第一部分中调整
    timearr[0]
  • 在大的
    if
    的第二部分中调整
    timearr[0]
    时,减法的顺序相反。(
    24-26===2
    ,您的意思可能是
    26-24==2
  • 最后,在检查
    if(month==12)
    之前,增加
    month+=1
    。要么需要在else中,要么必须检查
    month===13
  • ==
    更合适的地方使用
    ==
  • 您在声明
    timearr
    时忘记了
    var
  • 您选择了当前日期,但硬编码了时间
  • 总的来说,您似乎正在尝试以日/月/年格式输出UTC+5的当前日期。有很多更简单的方法来满足你的要求。例如:

    // 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;
    
    或与:


    请记住“时区!=偏移量”。碰巧的是,世界上所有目前使用UTC+5的地方(例如巴基斯坦)全年都在使用它,但是如果你说的是美国东部时间,一年中的某些时间将是UTC-5,而一年中的其他时间将是UTC-4

    • 您有一个箭头函数
      =>
      ,而不是大于或等于运算符
      =
      (感谢RobG)
    • 您将
      timearr
      视为一个数组,稍后将其视为一个数字。我想你的意思是比较
      timearr[0]
    • 当您添加5小时时,您正在连接字符串<代码>“19”+5==“195”
    。你需要在这里使用数字,而不是字符串
  • 您忘记在大型
    if
    语句的第一部分中调整
    timearr[0]
  • 在大的
    if
    的第二部分中调整
    timearr[0]
    时,减法的顺序相反。(
    24-26===2
    ,您的意思可能是
    26-24==2
  • 最后,在检查
    if(month==12)
    之前,增加
    month+=1
    。要么需要在else中,要么必须检查
    month===13
  • ==
    更合适的地方使用
    ==
  • 您在声明
    timearr
    时忘记了
    var
  • 您选择了当前日期,但硬编码了时间
  • 总的来说,您似乎正在尝试以日/月/年格式输出UTC+5的当前日期。有很多更简单的方法来满足你的要求。例如:

    // 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;
    
    或与:

    请记住“时区!=偏移量”。碰巧的是,世界上所有目前使用UTC+5的地方(例如巴基斯坦)全年都在使用它,但是如果你说的是美国东部时间,那么一年中的某些时间将是UTC-5,而一年中的其他时间将是UTC-4,timearr是一个数组,因此表达式始终为false。在
    if(timearr=>24)
    中,您正在计算一个箭头函数,因此它总是正确的。也许您的意思是
    =
    。在
    中,如果(timearr<0)
    ,则timearr是一个数组,因此表达式总是false。在
    if(timearr=>24)
    中,您正在计算一个箭头函数,因此它总是正确的。也许您的意思是
    =
    .Re”,以避免来自本地t的转换的干扰