JS中IST日期格式的毫秒数

时间:2016-07-13 21:29:13

标签: javascript date

我在IST中输入日期和时间,但它给出了UTC的毫秒数。如何在IST中获得毫秒数。

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

4 个答案:

答案 0 :(得分: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());

答案 1 :(得分: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>

答案 2 :(得分:0)

我不清楚你的期望是什么。使用偏移量解析ISO 8601日期和时间的功能并不困难(少于20行,SO上有示例)。无论主机设置如何,输出特定时区的日期和时间值也不困难。

如果您希望信任内置的Date解析器(不推荐使用),那么您可以创建一个符合ISO 8601标准的字符串,其中包含+05:30的偏移量(对于印度标准时间)并解析,例如< / p>

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

答案 3 :(得分:-1)

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