donglu1973 2017-11-23 09:31 采纳率: 100%
浏览 95

如果在AWS上使用golang生成的时间戳在本地系统上显示不同的日期时间

I have an angular application integrated with REST API developed with golang, on this app I have created a todo list functionality where I create todo for weekly or monthly, while creating todo I generate first timestamp with javascript and then submits it to API which created timestamp for other dates if its weekly or monthly todo. First timestamp is showing proper date time when I get it with API and show in using javascript date functions but other date's timestamps shows next day like if I save Wednesday then it shows Thursday.

As far as I know timestamps are not based on timezones so I chose timestamp instead of datetime

How can I resolve the issue ?

Following is my code to convert timestamp to date ( in required format )

let date = new Date(this.date*1000);

this.day = ("0" + date.getDate()).slice(-2);
this.month = ("0" + (date.getMonth()+1)).slice(-2);
this.year = date.getFullYear();
this.hours = ("0" + date.getHours()).slice(-2);
this.minutes = ("0" + date.getMinutes()).slice(-2);
this.seconds = ("0" + date.getSeconds()).slice(-2);
this.meridian = this.hours >= 12 ? 'pm' : 'am';
//Get name of the day and month
this.weekName = this.weekday[date.getDay()];
this.monthName = date.toLocaleString(this.locale, { month: "short" });
  • 写回答

1条回答 默认 最新

  • dsfsw1233 2017-11-23 10:04
    关注

    First off: I assume that this.weekday is an array containing the names of the week like this:

    this.weekday = [
      "monday",
      "tuesday",
      "wednesday",
      "thursday",
      "friday",
      "saturday",
      "sunday"
    ]
    

    This is a common way that people use to get the days of the week, but there are better ways.


    Using toLocaleDateString

    Vanilla Javascript has a nice way to get the name of the day and it even supports different locales!

    this.weekName = this.date.toLocaleDateString('en', { weekday: 'long' })
    

    Using an array with names

    If for some reason you wish to use an array anyway, you should know that arrays are zero-based and date.getDay() 1-based. In other words: your array counts from 0 to 6, while date.getDay() counts from 1 to 7.

    A simple way to get around this, is just adding 1 to the result of date.getDay()

    this.weekName = this.weekday[date.getDay() + 1];
    

    The rest of your variables can also be set using Date.toLocaleDateString, since it supports a lot of different outputs:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化