2016-07-13 14 views
-2

ISTで入力日時を指定していますが、UTCで表示されるミリ秒です。どのようにISTでミリ秒を得ることができますか?JSのIST日付形式のミリ秒数

var atPos = "03:00";  
var jsonDate = new Date("2016"+ "-0" + "7" + "-" + "14" + "T" + atPos); 
console.log(jsonDate.getTime()); // getting in UTC 
+0

あなたは、いくつかの大きな問題を抱えています。実装の相違により、Dateコンストラクタで文字列を解析することはお勧めできません。文字列が正しく解析された場合、それは "ローカル"として扱われます。つまり、ホストタイムゾーンオフセットを使用して同等のUTC時間値を使用するため、ホストがUTC +に設定されている場合にのみ "IST" 01:00。 – RobG

答えて

0

それはあなたが期待しているものを私に明確ではありません。 ISO 8601の日付と時刻をオフセットで解析する機能は、難しいことではありません(20行未満で、例があります)。また、ホストの設定に関係なく、特定のタイムゾーンの日付と時刻の値を出力することも困難です。

組み込みの日付パーサー(これはお勧めしません)を信頼したい場合は、+05:30(インド標準時)のオフセットを含むISO 8601に準拠した文字列を作成して解析できます。

var atPos = "03:00";  
var jsonDate = new Date("2016+ "-0" + "7" + "-" + "14" + "T" + atPos + "+05:30"); 

日付時刻値は、ECMA-262あたりUTCあり、あなたはあなたがそうするとき、それを変更することはできません。

console.log(jsonDate.getTime()); // getting in UTC 

あなたは常にUTC時刻値を取得します。ただし、その値を使用して(new Date(timevalue)のように)日付を作成すると、結果はホストの設定に関係なく同じ瞬間を表します。

以下は、任意のオフセットで日付文字列を出力する関数です。コメントが十分でない場合は、尋ねてください。 ;-)

/* Return an ISO 8601 long format string with the specified offset 
 
** 
 
** @param {Date} date - date to format 
 
** @param {string} offset - offset as "+hh:mm" or "+hhmm" or "Z" 
 
**       default is host time zone 
 
**       if sign is missing, default is "+" 
 
** @returns {string} ISO 8601 extended format 
 
*/ 
 
function formatISO(date, offset) { 
 
    function z(n) {return ('0' + n).slice(-2)} 
 
    function zz(n){return ('00' + n).slice(-3)} 
 
    if (!date) return; 
 
    // Copy date 
 
    var d = new Date(+date); 
 
    // Convert offset to minutes 
 
    var offSign = '+'; 
 
    if (typeof offset == 'undefined') { 
 
    offset = date.getTimezoneOffset(); 
 
    offSign = offset < 0? '+' : '-'; 
 
    offset *= -1; 
 
    } else if (/z/i.test(offset)) { 
 
    offset = 0; 
 
    } else { 
 
    offSign = /^-/.test(offset)? '-' : '+'; 
 
    var t = offset.match(/\d\d/g); 
 
    offset = (t[0]*60 + t[1]*1) * (offSign == '-'? -1 : 1); 
 
    } 
 
    
 
    // Adjust d to desired offset 
 
    d.setUTCMinutes(d.getUTCMinutes() + offset); 
 
    // Create timezone string 
 
    offset = Math.abs(offset); 
 
    offset = offset? offSign + z(offset/60 | 0) + ':' + z(offset % 60) : 'Z'; 
 
    
 
    // Create string 
 
    return d.getUTCFullYear() + '-' + 
 
     z(d.getUTCMonth() + 1) + '-' + 
 
     z(d.getUTCDate()) + 'T' + 
 
     z(d.getUTCHours()) + ':' + 
 
     z(d.getUTCMinutes()) + ':' + 
 
     z(d.getUTCSeconds()) + '.'+ 
 
     zz(d.getUTCMilliseconds()) + 
 
     offset; 
 
} 
 

 
console.log(formatISO(new Date()))   // Host 
 
console.log(formatISO(new Date(),'+05:30')) // IST 
 
console.log(formatISO(new Date(),'-0800')) // US AKDT 
 
console.log(formatISO(new Date(),'z'))  // UTC

0
sss Milliseconds 00 to 999 

document.writeln((new Date("2010")).toUTCString()); 

document.writeln((new Date("2010-06")).toUTCString()); 

document.writeln((new Date("2010-06-09")).toUTCString()); 

// Specifies Z, which indicates UTC time. 
document.writeln((new Date("2010-06-09T15:20:00Z")).toUTCString()); 

// Specifies -07:00 offset, which is equivalent to Pacific Daylight time. 
document.writeln((new Date("2010-06-09T15:20:00-07:00")).toGMTString()); 

// Specifies a non-ISO Long date. 
document.writeln((new Date("June 9, 2010")).toUTCString()); 

// Specifies a non-ISO Long date. 
document.writeln((new Date("2010 June 9")).toUTCString()); 

// Specifies a non-ISO Short date and time. 
document.writeln((new Date("6/9/2010 3:20 pm")).toUTCString()); 
+0

これは私に火曜日の6月14日2016 18:30:00を与えるが、それは私に与える必要があります火曜日Jun 14 2016 14:00:00 –

+0

私はあなたがあなたが精巧なことができます – Ankit

0
store that millisecond into a variable and then use new date function your job would be done 
check out this to convert into millisecond 
<!DOCTYPE html> 
<html> 
<body> 

<p>Click the button to display the numbers of milliseconds between a specified date and midnight January 1, 1970.</p> 

<button onclick="myFunction()">Try it</button> 

<p id="demo"></p> 

<script> 
function myFunction() { 
    var d = Date.UTC(2012, 02, 30); 
    document.getElementById("demo").innerHTML = d; 
    var milliseconds= document.getElementById(demo); 
    var d = new Date(milliseconds); 
    alert(d.toUTCString()); 
} 
</script> 

</body> 
</html> 
-1
<!DOCTYPE html> 
<html> 
    <body>  
     <p id="demo"></p>  
      <script> 
       document.getElementById("demo").innerHTML = new Date("2015-03"); 
      </script>  
    </body> 
</html>