代码之家  ›  专栏  ›  技术社区  ›  DCR

js函数适用于pc桌面,但不适用于ios iphone

  •  0
  • DCR  · 技术社区  · 4 年前

    我有下面的片段,适用于我的电脑桌面。它在那里工作,但在我的iphone上不工作。

    function dateFormat(date,type){    
        
        let dayOfWeek = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
        let month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
        date.replaceAll("-","/");   
        
        date = new Date(date + " EDT");     
        
        function daySuffix(d) {
          if (d > 3 && d < 21) return d + 'th';
          switch (d % 10) {
            case 1:  return d + "st";
            case 2:  return d + "nd";
            case 3:  return d + "rd";
            default: return d + "th";
          }
        }
        let day = daySuffix(date.getDate());
        
        let string = "";
        
        switch(type){
           case 'ld': string = dayOfWeek[date.getDay()] + ", " + month[date.getMonth()] + " " + day;break;
           case 'sd': string =  month[date.getMonth()] + " " + day;break;
           default: string =  "wrong type, ld or sd only";
        }
        console.log(string,'string');
        return string;
     }
     
     console.log(dateFormat("2022-05-06","ld"))
    0 回复  |  直到 4 年前
        1
  •  0
  •   DCR    4 年前

    问题就出在这一行:

    date.replaceAll("-","/");
    

    它需要:

    date = date.replaceAll("-","/");
    

    function dateFormat(date,type){    
        
        let dayOfWeek = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
        let month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
        date = date.replaceAll("-","/");   
        
        date = new Date(date + " EDT");     
        
        function daySuffix(d) {
          if (d > 3 && d < 21) return d + 'th';
          switch (d % 10) {
            case 1:  return d + "st";
            case 2:  return d + "nd";
            case 3:  return d + "rd";
            default: return d + "th";
          }
        }
        let day = daySuffix(date.getDate());
        
        let string = "";
        
        switch(type){
           case 'ld': string = dayOfWeek[date.getDay()] + ", " + month[date.getMonth()] + " " + day;break;
           case 'sd': string =  month[date.getMonth()] + " " + day;break;
           default: string =  "wrong type, ld or sd only";
        }
        console.log(string,'string');
        return string;
     }
     
     console.log(dateFormat("2022-05-06","ld"))
        2
  •  0
  •   Serhii Zasenko    4 年前

    问题出在附加到日期字符串的时区上 EDT .它正在生成错误的日期字符串 "2022-05-06 EDT" 。 您可以通过格式化日期并将其解析回来来存档设置时区(但它对仅限日期的日期没有影响)。

    function dateFormat(date,type){    
        
        let dayOfWeek = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
        let month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; 
        
        date = new Date(new Date(date).toLocaleString('en-US', {
          timeZone: "America/Detroit"
        }));
        
        function daySuffix(d) {
          if (d > 3 && d < 21) return d + 'th';
          switch (d % 10) {
            case 1:  return d + "st";
            case 2:  return d + "nd";
            case 3:  return d + "rd";
            default: return d + "th";
          }
        }
        let day = daySuffix(date.getDate());
        
        let string = "";
        
        switch(type){
           case 'ld': string = dayOfWeek[date.getDay()] + ", " + month[date.getMonth()] + " " + day;break;
           case 'sd': string =  month[date.getMonth()] + " " + day;break;
           default: string =  "wrong type, ld or sd only";
        }
        console.log(string,'string');
        return string;
     }
     
     console.log(dateFormat("2022-05-06","ld"))