2017-04-13 6 views
0

私はバックエンドサービスコールから日付の値を取得する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でこれを達成できますか?これを達成するにはMoment.jsとmoment-timezone.jsが必要ですか?JavaScriptでローカル日付と同等の日付に変換する方法

答えて

1

ローカルタイムゾーンのUTCからのオフセット時間を示すタイムゾーンオフセットを試すことができます。

let myDate = new Date('2017-04-13'); 
myDate = new Date(myDate.getTime() + (myDate.getTimeZoneOffset() * 60 * 1000)); 

代わりに私は、私はあなたのユースケースで作業する前に、それはここで少しやり過ぎかもしれ使用のだが、それが動作古い機能リファクタリング:いいえ、Moment.jsではありません

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 
1

を必須。私は同じ問題を持っていたとJavascriptの日付パーサは異なり、それを解析することができるように、日付の形式を変更する$filterを使用することにしました:

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) 
1

Why does Date.parse give incorrect resultsを設定します。その答えは、DateコンストラクタやDate.parseを使用して文字列を解析しない理由を示しています。

ISO 8601形式の日付をローカルとして確実に解析するには、単純な関数が必要です。また、次の例では値が検証され、月または日が範囲外の場合は無効な日付が返されます。あなたは時間が正午になりたい場合は、12

function parseISOLocal(s) { 
 
    var b = s.split(/\D/); 
 
    var d = new Date(b[0], --b[1], b[2]); 
 
    return d && d.getMonth() == b[1]? d : new Date(NaN); 
 
} 
 

 
var d = parseISOLocal('2017-04-13'); 
 
d.setHours(12); 
 
console.log(d.toString());

に時間を設定します
関連する問題