Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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_Angular_Html_Typescript_Timeago - Fatal编程技术网

Javascript 日期以奇怪的方式显示

Javascript 日期以奇怪的方式显示,javascript,angular,html,typescript,timeago,Javascript,Angular,Html,Typescript,Timeago,我在angular中有一个应用程序,用户可以输入评论,评论会自动显示并显示添加的日期,奇怪的部分如下 当用户输入一个参数时,这里显示的是什么(检查时间) 第一个命令是昨天发出的,第二个命令是你在一小时前而不是现在提交了一个日期后可以看到的新命令 当用户刷新浏览器时,此处显示的是什么?检查顶部注释时间? 我想要的是旧的评论应该在顶部,新的评论应该在底部 新的评论现在应该有时间,而不是在刷新之后 这是我的解决方案,注意用于日期格式化的am momet js 函数在ts中添加和获取注释 // ge

我在angular中有一个应用程序,用户可以输入评论,评论会自动显示并显示添加的日期,奇怪的部分如下

  • 当用户输入一个参数时,这里显示的是什么(检查时间)
  • 第一个命令是昨天发出的,第二个命令是你在一小时前而不是现在提交了一个日期后可以看到的新命令

  • 当用户刷新浏览器时,此处显示的是什么?检查顶部注释时间?
  • 我想要的是旧的评论应该在顶部,新的评论应该在底部 新的评论现在应该有时间,而不是在刷新之后

    这是我的解决方案,注意用于日期格式化的am momet js

    函数在ts中添加和获取注释

    // get all comments
        this.activeRouter.params.subscribe((params) => {
            const id = params['id'];
            this.userService.getComments(id)
            .pipe(
              map(data => data.sort((a, b) => new Date(b.localTime).getTime() - new Date(a.localTime).getTime()))
            )
            .subscribe(data => this.comments = data);
         });
      }
    
    // add comments to server
      addComments(task_id) {
        const formData = this.addForm.value;
        formData.task_id = task_id;
        this.userService.addComments(formData)
        .subscribe(data => {
          this.comments.push(this.addForm.value);
          this.addForm.reset();
        });
      // grab localtime
        const date = new Date();
        const d = date.getUTCDate();
        const day = (d < 10) ? '0' + d : d;
        const m = date.getUTCMonth() + 1;
        const month = (m < 10) ? '0' + m : m;
        const year = date.getUTCFullYear();
        const h = date.getUTCHours();
        const hour = (h < 10) ? '0' + h : h;
        const mi = date.getUTCMinutes();
        const minute = (mi < 10) ? '0' + mi : mi;
        const sc = date.getUTCSeconds();
        const second = (sc < 10) ? '0' + sc : sc;
        const loctime = `${year}-${month}-${day}T${hour}`;
    
        this. addForm.get('localTime').setValue(loctime);
    
      }
    
    这里是HTML

     <span class="days">{{comment.localTime | amTimeAgo}}</span>
    
    {{comment.localTime | amTimeAgo}
    
    注意:为了获得这些时间之前,horu-ago等,我使用管道:


    在我的代码中我做错了什么????谢谢,这是因为发送到服务器并立即使用的值是不同的。请注意,您直接使用
    loctime
    变量,该变量在一些转换后保存一个值,但您只向服务器发送一个
    新日期()
    。在这里我记录了这两个值,请注意差异:

    const date=新日期();
    const d=date.getUTCDate();
    施工日=(d<10)?'0'+d:d;
    常量m=date.getUTCMonth()+1;
    施工月=(m<10)?'0'+m:m;
    const year=date.getUTCFullYear();
    const h=date.getUTCHours();
    常数小时=(h<10)?'0'+h:h;
    const mi=date.getUTCMinutes();
    常数分钟=(mi<10)?'0'+mi:mi;
    const sc=date.getUTCSeconds();
    常数秒=(sc<10)?'0'+sc:sc;
    const loctime=`${year}-${month}-${day}T${hour}`;
    console.log('日期:',日期);
    log('loctime:',loctime);
    console.log('typeof date:',typeof date);
    
    log('typeof loctime:',typeof loctime)这解决了时间问题,但是新注释显示在顶部而不是底部如何?
      "comment": [
        {
          "id": 1,
          "localTime": "2018-10-29T23:12:57.129Z",
          "description": "Putin is Dope\n"
        },
        {
          "id": 2,
          "localTime": "2018-10-29T23:13:25.743Z",
          "description": "Obama is cool \n"
        },
      {
          "id": 2,
          "localTime": "2018-10-29T23:13:25.743Z",
          "description": "No No do U know JOHN POMBE MAGUFULI? the president of Tanzania oooh oooh man he is savage , they call him Buldoser, Moderator please change the title "Who is the weakest president of all time?" => OBAMA  \n"
        }
    
      ]
    }
    
     <span class="days">{{comment.localTime | amTimeAgo}}</span>