[英]How to Convert Hard Coded date into standard GMT format
I need to convert a hard coded date into a standard GMT format.How can I do this? 我需要将硬编码日期转换为标准GMT格式。我该怎么做? The date I have is in the following format: var myDate = 'dd|mm|yyyy'; 我的日期格式如下:var myDate ='dd | mm | yyyy'; There is no time or day description in the date.Just the 'dd|mm|yyyy' string. 日期中没有时间或日期描述。只有'dd | mm | yyyy'字符串。 Is there a way I can convert it into GMT? 有没有办法可以将它转换成GMT?
Thanks in advance. 提前致谢。
a = '22/02/2014'.split('/')
d = new Date(a[2],parseInt(a[1], 10) - 1,a[0])
//Sat Feb 22 2014 00:00:00 GMT+0530 (India Standard Time)
Now you have a javascript date object in d
现在你在d
有一个javascript日期对象
utc = d.getUTCDate() + "/" + (d.getUTCMonth() + 1 ) + "/" + d.getUTCFullYear();
//"21/2/2014" for an accurate conversion to UTC time of day is a must.
If you are in say India, the Javascript Date
object will have timeZoneOffset
330
. 如果您在印度,则Javascript Date
对象将具有timeZoneOffset
330
。 So its not possible to keep a javascript Date
object with timezone GMT
unless your system time is GMT
. 因此,除非您的系统时间是GMT
否则无法使用时区GMT
保留javascript Date
对象。
So if you want a Date
object for calculation, you can create one with localTimezone and simply suppose it is GMT
因此,如果您想要一个Date
对象进行计算,您可以使用localTimezone创建一个并简单地假设它是GMT
pseudoGMT = new Date( Date.parse(d) + d.getTimezoneOffset() * 60 * 1000);
//Fri Feb 21 2014 18:30:00 GMT+0530 (India Standard Time)
If you can explain your high level requirement we might be able to help with some alternate solutions. 如果您能解释您的高级别要求,我们可以帮助您解决其他问题。
Use regex matching to extract the data you need: 使用正则表达式匹配来提取所需的数据:
var myDate = "21|01|2014";
var data = myDate.match(/(\d{2})\|(\d{2})\|(\d{4})/);
var date = new Date(data[3], data[2] - 1, data[1]);
Note that the month is 0-indexed, so january = 0 请注意,月份为0索引,因此january = 0
More on regular expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp 有关正则表达式的更多信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.