简体   繁体   English

如何将硬编码日期转换为标准GMT格式

[英]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.

相关问题 如何将日期转换为标准格式 - How to convert Date into Standard format 如何将日期格式(2018年6月22日星期五14:37:47 GMT + 0530(印度标准时间))转换为DD-MM-YYYY - How to convert date format(Fri Jun 22 2018 14:37:47 GMT+0530 (India Standard Time)) to DD-MM-YYYY 如何将日期转换成React JS中的日期-2019年2月27日星期三11:11:16 GMT + 0530(印度标准时间)转换为2019年2月27日 - How to convert date — Wed Feb 27 2019 11:11:16 GMT+0530 (India Standard Time) to 27-Feb-2019 format in React JS 如何将短日期转换为以下格式:DDD MMM DD YYYY GMT + 0800(X国家标准时间)? - How to convert short dates to this format: DDD MMM DD YYYY GMT+0800 (X country standard time)? 无法将日期格式转换为2018年7月23日星期一21:54:14 GMT + 0530(印度标准时间)为YYYYMMDD - unable to convert date format Mon Jul 23 2018 21:54:14 GMT+0530 (India Standard Time) to YYYYMMDD Javascript Date():如何将本地日期转换为GMT - Javascript Date(): How to convert the local date to GMT 如何使用JS将GMT日期时间转换为GMT Unix TimeStamp? - How to convert GMT date time into GMT Unix TimeStamp using JS? 如何在if语句中将日期输入与硬编码日期进行比较? - How to compare input of date to hard coded date in if statement? 以Javascript新Date(GMT)格式转换数据库日期时间或日期 - Convert Database datetime or date in Javascript new Date(GMT) format 如何使用AngularJs中的矩将Unix时间戳转换为GMT(IST)格式的日期 - How to convert Unix Timestamp to date with GMT (IST) format using moment in AngularJs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM