Ich habe ein Datum im Format von:
2010-11-30T08:32:22+0000
2010-11-27T20:59:17+0000
aus einem Feed, im String-Format zu Javascript, jetzt möchte ich es in ein Date-Objekt konvertieren. Wie kann ich es in javascript tun?
Antworten
Zu viele Anzeigen?
Mahdi Abdi
Punkte
672
Bruno Calza
Punkte
2664
user2166413
Punkte
1
Ich fand Fall wie Date.fromISOString('2011-06-01T00:00:00Z') erstellt Datum, das durch Zeitzone versetzt ist, so dass dies nur korrekt funktioniert, wenn Ihr Computer in GMT ist. Hier ist die Änderung, die ich gemacht habe, um das zu beheben.
Um ein korrektes Datum aus einer beliebigen ISO-Zeichenkette zu erhalten, müssen Sie die lokale Zeitzone anpassen, auch wenn der angegebene Eintrag GMT ist.
Hier ist ein funktionierendes Beispiel, das Sie als .html-Datei speichern und auf Ihrem Computer testen können:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
<title>ISO Dates String to Date</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" language="JavaScript">
// Modified from: http://stackoverflow.com/questions/4450837/javascript-string-to-date-php-iso-string-format
// Thank you Roman Dvornov
Date.fromISOString = (function(){
var tzoffset = (new Date).getTimezoneOffset();
function fastDateParse(y, m, d, h, i, s, ms){ // this -> tz
return new Date(y, m - 1, d, h || 0, +(i || 0) - this, s || 0, ms || 0);
}
// result function
return function(isoDateString){
var tz = isoDateString.substr(10).match(/([\-\+])(\d{1,2}):?(\d{1,2})?/) || 0;
if (tz) {
tz = tzoffset + (tz[1] == '-' ? -1 : 1) * (tz[3] != null ? +tz[2] * 60 + (+tz[3]) : +tz[2]);
} else {
tz = tzoffset;
}
return fastDateParse.apply(tz || 0, isoDateString.split(/\D/));
}
})();
function checkDate() {
showDateIso($('#IsoStringInput').val());
}
function dates() {
showDateIso('2011-06-01');
showDateIso('2011-06-01T00:00:00');
showDateIso('2011-06-01T00:00:00Z');
showDateIso('2011-06-01T00:00:00+30');
showDateIso('2011-06-01T00:00:00-30');
showDateIso('2011-06-01T00:00:00+0530');
showDateIso('2011-06-01T00:00:00-0530');
showDateIso('2011-06-01T00:00:00+05:30');
showDateIso('2011-06-01T00:00:00-05:30');
}
function showDateIso(isoString) {
var $tr = $('<tr/>').prependTo($('#DatesTable').find('tbody'));
$('<td/>').appendTo($tr).text(isoString);
var isoDate = Date.fromISOString(isoString);
$('<td/>').appendTo($tr).text(isoDate);
var now = new Date();
$('<td/>').appendTo($tr).text(now);
$('<td/>').appendTo($tr).text(showTimeDiff(now.getTime() - isoDate.getTime()));
$tr.fadeOut(100).fadeIn(1400);
}
var ONE_YEAR_MS = 31536000000;
var ONE_WEEK_MS = 604800000;
var ONE_DAY_MS = 86400000;
var ONE_HOUR_MS = 3600000;
var ONE_MINUTE_MS = 60000;
function showTimeDiff(timeMs) {
var result = '';
if (typeof(timeMs) !== 'undefined') {
var years = 0;
while (timeMs >= ONE_YEAR_MS) {
years = years + 1;
timeMs = timeMs - ONE_YEAR_MS;
}
if (years > 0) {
result = result + years + ((weeks === 1) ? ' year ' : ' years ');
}
var weeks = 0;
while (timeMs >= ONE_WEEK_MS) {
weeks = weeks + 1;
timeMs = timeMs - ONE_WEEK_MS;
}
if (weeks > 0) {
result = result + weeks + ((weeks === 1) ? ' week ' : ' weeks ');
}
var days = 0;
while (timeMs >= ONE_DAY_MS) {
days = days + 1;
timeMs = timeMs - ONE_DAY_MS;
}
if (days > 0) {
result = result + days + ((days === 1) ? ' day ' : ' days ');
}
var hours = 0;
while (timeMs >= ONE_HOUR_MS) {
hours = hours + 1;
timeMs = timeMs - ONE_HOUR_MS;
}
var minutes = 0;
while (timeMs >= ONE_MINUTE_MS) {
minutes = minutes + 1;
timeMs = timeMs - ONE_MINUTE_MS;
}
// Break down to seconds
var seconds = parseInt(timeMs / 1000, 10);
if (hours > 0) {
result = result + hours + ':' + pad(minutes, 2) + ':' + pad(seconds, 2);
} else if (minutes > 0) {
result = result + minutes + ':' + pad(seconds, 2);
} else if (seconds > 0) {
result = result + seconds + ' sec';
}
}
return result;
}
function pad(number, len) {
var result = '';
if (!isNaN(number)) {
result = '' + number;
while (result.length < len) {
result = '0' + result;
}
}
return result;
}
</script>
</head>
<body>
<div>
<label>ISO String:</label>
<input id="IsoStringInput" type="text" value="" style="width: 300px;"/>
</div>
<button type="button" onclick="checkDate();">Check Entered Date</button>
<button type="button" onclick="dates();">ISO for June 01, 2011</button>
<table id="DatesTable">
<thead>
<th>ISO String</th>
<th>Date Printed <span style="color: #888888;">(Local Timezone)</span></th>
<th>Now</th>
<th>Now - ISO <span style="color: #888888;">(minutes)</span></th>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
- See previous answers
- Weitere Antworten anzeigen