如何在JavaScript中将日期转换为本地等效日期

如何在JavaScript中将日期转换为本地等效日期,javascript,angularjs,date,momentjs,Javascript,Angularjs,Date,Momentjs,我有一个AngularJS,从后端服务调用中获取日期值。我从后端收到的日期是一个字符串。例如“2017-04-13”。当我尝试在UI上显示时,javascript将其转换为本地时间“2017-04-12 17:00:00 CST”,因为在UI上显示的是一天的休息时间。我试图实现的是根据客户的时区显示“2017-04-13 12:00:00 EST”或“2017-04-13 12:00:00 PST”或“2017-04-13 12:00:00 CST”等。这样日期就不会改变,休息日也不会显示在UI

我有一个AngularJS,从后端服务调用中获取日期值。我从后端收到的日期是一个字符串。例如“2017-04-13”。当我尝试在UI上显示时,javascript将其转换为本地时间“2017-04-12 17:00:00 CST”,因为在UI上显示的是一天的休息时间。我试图实现的是根据客户的时区显示“2017-04-13 12:00:00 EST”或“2017-04-13 12:00:00 PST”或“2017-04-13 12:00:00 CST”等。这样日期就不会改变,休息日也不会显示在UI上。如何在Javascript中实现这一点。是否需要使用矩量.js和矩量-timezone.js来实现这一点?

您可以尝试使用时区偏移量,该偏移量给出本地时区与UTC的分钟偏移量

let myDate = new Date('2017-04-13');
myDate = new Date(myDate.getTime() + (myDate.getTimeZoneOffset() * 60 * 1000));
或者,我重构了一个以前用于处理您的用例的旧函数,这可能有点过分,但它可以工作:

Date.prototype.format = function(format) {
    var result = "";

    format = format.replace("mm", this.getUTCMonth() + 1 < 10 ? "0" + (this.getUTCMonth() + 1) : this.getUTCMonth() + 1);
    format = format.replace("m", this.getUTCMonth() + 1);
    format = format.replace("dd", this.getUTCDate() < 10 ? "0" + this.getUTCDate() : this.getUTCDate());
    format = format.replace("d", this.getUTCDate());
    format = format.replace("yyyy", this.getUTCFullYear());
    format = format.replace("yy", String(this.getUTCFullYear()).substring(String(this.getUTCFullYear()).length - 2));

    format = format.replace("HH", this.getUTCHours() < 10 ? "0" + this.getUTCHours() : this.getUTCHours());
    format = format.replace("H", this.getUTCHours());
    format = format.replace("hh", this.getUTCHours() == 0 ? "12" : this.getUTCHours() < 10 ? "0" + this.getUTCHours() : this.getUTCHours());
    format = format.replace("h", this.getUTCHours() == 0 ? "12" : this.getUTCHours());
    format = format.replace("nn", this.getUTCMinutes() < 10 ? "0" + this.getUTCMinutes() : this.getUTCMinutes());
    format = format.replace("n", this.getUTCMinutes());
    format = format.replace("ss", this.getUTCSeconds() < 10 ? "0" + this.getUTCSeconds() : this.getUTCSeconds());
    format = format.replace("s", this.getUTCSeconds());
    format = format.replace("p", this.getUTCHours() >= 12 ? "PM" : "AM");
    format = format.replace("t", new Date().toString().match(/\(([A-Za-z\s].*)\)/)[1]);

    return format;
};

let myDate = new Date('2017-04-13');
myDate.format('yyyy-mm-dd HH:mm:ss t');
// Output -> 2017-04-13 00:00:00 EDT
Date.prototype.format=函数(格式){
var结果=”;
format=format.replace(“mm”,this.getUTCMonth()+1<10?“0”+(this.getUTCMonth()+1):this.getUTCMonth()+1);
format=format.replace(“m”,this.getUTCMonth()+1);
format=format.replace(“dd”,this.getUTCDate()<10?“0”+this.getUTCDate():this.getUTCDate());
format=format.replace(“d”,this.getUTCDate());
format=format.replace(“yyyy”,this.getUTCFullYear());
format=format.replace(“yy”,字符串(this.getUTCFullYear()).substring(字符串(this.getUTCFullYear()).length-2));
format=format.replace(“HH”,this.getUTCHours()<10?“0”+this.getUTCHours():this.getUTCHours());
format=format.replace(“H”,this.getUTCHours());
format=format.replace(“hh”,this.getUTCHours()==0?“12”:this.getUTCHours()<10?“0”+this.getUTCHours():this.getUTCHours());
format=format.replace(“h”,this.getUTCHours()==0?“12”:this.getUTCHours());
format=format.replace(“nn”,this.getUTCMinutes()<10?“0”+this.getUTCMinutes():this.getUTCMinutes());
format=format.replace(“n”,this.getUTCMinutes());
format=format.replace(“ss”,this.getUTCSeconds()<10?“0”+this.getUTCSeconds():this.getUTCSeconds());
format=format.replace(“s”,this.getUTCSeconds());
format=format.replace(“p”,this.getUTCHours()>=12?”PM:“AM”);
format=format.replace(“t”,new Date().toString().match(/\([A-Za-z\s].*))/)[1]);
返回格式;
};
让myDate=新日期(“2017-04-13”);
格式('yyyy-mm-dd HH:mm:ss t');
//输出->2017-04-13美国东部夏令时00:00:00

您可以尝试使用时区偏移量,它提供了本地时区与UTC的分钟偏移量

let myDate = new Date('2017-04-13');
myDate = new Date(myDate.getTime() + (myDate.getTimeZoneOffset() * 60 * 1000));
或者,我重构了一个以前用于处理您的用例的旧函数,这可能有点过分,但它可以工作:

Date.prototype.format = function(format) {
    var result = "";

    format = format.replace("mm", this.getUTCMonth() + 1 < 10 ? "0" + (this.getUTCMonth() + 1) : this.getUTCMonth() + 1);
    format = format.replace("m", this.getUTCMonth() + 1);
    format = format.replace("dd", this.getUTCDate() < 10 ? "0" + this.getUTCDate() : this.getUTCDate());
    format = format.replace("d", this.getUTCDate());
    format = format.replace("yyyy", this.getUTCFullYear());
    format = format.replace("yy", String(this.getUTCFullYear()).substring(String(this.getUTCFullYear()).length - 2));

    format = format.replace("HH", this.getUTCHours() < 10 ? "0" + this.getUTCHours() : this.getUTCHours());
    format = format.replace("H", this.getUTCHours());
    format = format.replace("hh", this.getUTCHours() == 0 ? "12" : this.getUTCHours() < 10 ? "0" + this.getUTCHours() : this.getUTCHours());
    format = format.replace("h", this.getUTCHours() == 0 ? "12" : this.getUTCHours());
    format = format.replace("nn", this.getUTCMinutes() < 10 ? "0" + this.getUTCMinutes() : this.getUTCMinutes());
    format = format.replace("n", this.getUTCMinutes());
    format = format.replace("ss", this.getUTCSeconds() < 10 ? "0" + this.getUTCSeconds() : this.getUTCSeconds());
    format = format.replace("s", this.getUTCSeconds());
    format = format.replace("p", this.getUTCHours() >= 12 ? "PM" : "AM");
    format = format.replace("t", new Date().toString().match(/\(([A-Za-z\s].*)\)/)[1]);

    return format;
};

let myDate = new Date('2017-04-13');
myDate.format('yyyy-mm-dd HH:mm:ss t');
// Output -> 2017-04-13 00:00:00 EDT
Date.prototype.format=函数(格式){
var结果=”;
format=format.replace(“mm”,this.getUTCMonth()+1<10?“0”+(this.getUTCMonth()+1):this.getUTCMonth()+1);
format=format.replace(“m”,this.getUTCMonth()+1);
format=format.replace(“dd”,this.getUTCDate()<10?“0”+this.getUTCDate():this.getUTCDate());
format=format.replace(“d”,this.getUTCDate());
format=format.replace(“yyyy”,this.getUTCFullYear());
format=format.replace(“yy”,字符串(this.getUTCFullYear()).substring(字符串(this.getUTCFullYear()).length-2));
format=format.replace(“HH”,this.getUTCHours()<10?“0”+this.getUTCHours():this.getUTCHours());
format=format.replace(“H”,this.getUTCHours());
format=format.replace(“hh”,this.getUTCHours()==0?“12”:this.getUTCHours()<10?“0”+this.getUTCHours():this.getUTCHours());
format=format.replace(“h”,this.getUTCHours()==0?“12”:this.getUTCHours());
format=format.replace(“nn”,this.getUTCMinutes()<10?“0”+this.getUTCMinutes():this.getUTCMinutes());
format=format.replace(“n”,this.getUTCMinutes());
format=format.replace(“ss”,this.getUTCSeconds()<10?“0”+this.getUTCSeconds():this.getUTCSeconds());
format=format.replace(“s”,this.getUTCSeconds());
format=format.replace(“p”,this.getUTCHours()>=12?”PM:“AM”);
format=format.replace(“t”,new Date().toString().match(/\([A-Za-z\s].*))/)[1]);
返回格式;
};
让myDate=新日期(“2017-04-13”);
格式('yyyy-mm-dd HH:mm:ss t');
//输出->2017-04-13美国东部夏令时00:00:00

否,不需要Moment.js。我也遇到了同样的问题,决定使用
$filter
更改日期的格式,以允许Javascript日期解析器以不同的方式对其进行解析:

var correctDate = new Date($filter('date')(backEndDate, 'MMMM d, yyyy'));
因此,不要解析:

new Date('2017-04-13') // Wed Apr 12 2017 19:00:00 GMT-0500 (Central Daylight Time)
您正在分析:

new Date('April 13, 2017');  //Thu Apr 13 2017 00:00:00 GMT-0500 (Central Daylight Time)

不需要,不需要Moment.js。我也遇到了同样的问题,决定使用
$filter
更改日期的格式,以允许Javascript日期解析器以不同的方式对其进行解析:

var correctDate = new Date($filter('date')(backEndDate, 'MMMM d, yyyy'));
因此,不要解析:

new Date('2017-04-13') // Wed Apr 12 2017 19:00:00 GMT-0500 (Central Daylight Time)
您正在分析:

new Date('April 13, 2017');  //Thu Apr 13 2017 00:00:00 GMT-0500 (Central Daylight Time)

由于日期不正确的原因,请设置。这个答案还指出了为什么不应该使用Date构造函数或Date.parse解析字符串

要可靠地将ISO 8601格式的日期解析为本地日期,需要一个简单的函数。如果月份或日期超出范围,以下内容还会验证这些值并返回无效日期。如果希望时间为中午,则将小时数设置为12

函数解析本地{
var b=s.split(/\D/);
var d=新日期(b[0],--b[1],b[2]);
返回d&d.getMonth()==b[1]?d:新日期(NaN);
}
var d=parseISOLocal('2017-04-13');
d、 设定时间(12);

log(d.toString())有关日期不正确的原因,请设置。这个答案还指出了为什么不应该使用Date构造函数或Date.parse解析字符串

要可靠地将ISO 8601格式的日期解析为本地日期,需要一个简单的函数。如果月份或日期超出范围,以下内容还会验证这些值并返回无效日期。如果你想把时间定在中午,那么