首页 文章

无法在Angular 2/4中获得本月的第一天

提问于
浏览
1

我正在计算我的申请日期,如日期,星期,月份 .

首先我定义日,周,月,定制如:

this.reportTypes = [{TypeId: 1, Type: 'Day'}, {TypeId: 6, Type: 'Week'}, {TypeId: 30, Type: 'Month'}, {
      TypeId: 10,
      Type: 'Custom'
    }]

接下来我定义的日期如下:

var currdate = new Date();
 if(reportType==1){
        // this.reportDataFromDate=currdate;
        // this.reportDataToDate=currdate;
        //This is for setting the current date
        this.reportDataFromDate= currdate;
        this.reportDataToDate= currdate;
    }
    else if(reportType==30){
      var First = new Date(currdate.getFullYear(),currdate.getMonth(),1);

      this.reportDataFromDate=First;

      this.reportDataToDate=currdate;
    }
    else if(reportType!=10){
       var last = new Date(currdate.getTime() - (reportType * 24 * 60 * 60 * 1000));

      this.reportDataFromDate=last;
      this.reportDataToDate=currdate;
    }
  }

问题是在选择 reportType == 30 之后它必须得到该月的第一天 .

它显示的日期为 1-Dec-2017 但是它将获取截至2017年11月30日的数据?

这是SQL服务器的屏幕截图 . 我发送日期为2017年12月1日,但它将在30-11-2017 .

3 回答

  • 3

    为什么不同?

    new Date(x,y,z) 构造函数将参数视为 local date values .

    MDB Web Docs - Date

    注意:如果将Date作为具有多个参数的构造函数调用,则指定的参数表示本地时间 . 如果需要UTC,请使用具有相同参数的新日期(Date.UTC(...)) .

    但是,在引擎盖下,日期存储为UTC(自1970年1月1日以来的毫秒数) .

    const date = new Date(2017, 11, 29); 
    console.log('valueOf()', date.valueOf())  // 1514458800000
    

    并且UTC日期与您的本地日期不同(请参阅尾随'Z'表示UTC)

    const date = new Date(2017, 11, 29); 
    console.log('date', date)  // "2017-12-28T11:00:00.000Z" (trailing 'Z' means UTC)
    
    // The difference in minutes between browser local and UTC
    console.log('getTimezoneOffset()', date.getTimezoneOffset() )
    

    当您将其发送到服务器时,JSON将其作为UTC发送

    const date = new Date(2017, 11, 29); 
    console.log('JSON', date.toJSON())
    // JSON will yield string version of UTC === 2017-12-28T11:00:00.000Z
    

    如何修复它

    好吧,你可能会认为你确实想要在当地的日期/时间,并得出结论它没有被打破 .

    但是如果你想将 send UTC发送到服务器,请将参数包装在 Date.UTC()

    const date = new Date(Date.UTC( 2017, 11, 29 ))
    console.log('date.toJSON()', date.toJSON() )   // 2017-12-29T00:00:00.000Z
    

    月参数怎么样=== 11?

    从上面引用的MDB页面,

    注意:参数月份为0 . 这意味着1月= 0和12月= 11 .

  • 2

    当使用整数调用Date()构造函数时,结果是一个日期对象,该日期假定您的系统(读取浏览器/ os)时区 .

    例:

    let d = new Date(2017);
    // returns Thu Jan 01 1970 01:00:02 GMT+0100 (W. Europe Standard Time)
    // and with d.toUTCString(): Fri, 30 Dec 2016 23:00:00 GMT
    

    这可能会在发送到服务器的整个不同年份结束

    使用字符串构造函数并指定时区将帮助您克服这个问题 .

    例:

    let d = new Date('2017z');
    // returns Sun Jan 01 2017 01:00:00 GMT+0100 (W. Europe Standard Time)
    // and with d.toUTCString(): Sun, 01 Jan 2017 00:00:00 GMT
    

    后者是你应该传递给服务器的,通常做计算 .

    但请注意,使用日期计算是一个复杂的问题,最好留给像moment.js这样的库 . 要了解您正在处理的内容,请查看WebRebel Session 中的great talk .

    因此,要实际给出 Headers 的答案,请尝试使用UTC在简单字符串中创建日期的示例:

    let d = new Date(currdate.getUTCFullYear() + ' ' +(currdate.getUTCMonth() + 1) + ' 1z');
    d.getUTCDay(); // returns the day as an integer where Monday is 0.
    

    请注意,由于getUTCMonth()返回1月为0,我们添加1个月 .

  • 1

    如果您使用.Net Web API作为后端,则可以在Web API WebApiconfig.cs中配置时区,如下所示 . 它将以UTC格式化时间 .

    public static void Register(HttpConfiguration config)
    {
        config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
    }
    

    或者使用

    config.Formatters.JsonFormatter.SerializerSettings.DateTimeZ‌​oneHandling = Newtonsoft.Json.DateTimeZoneHandling.RoundtripKind; //Time zone information should be preserved when converting.
    

相关问题