将JavaScript DateTime转换为UTC,无需毫秒 [英] Convert JavaScript DateTime to UTC with no milliseconds

查看:276
本文介绍了将JavaScript DateTime转换为UTC,无需毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在尝试将以人类可读格式编写的日期解析为SharePoint列表将接受的DateTime字符串.为了做到这一点,我确定我需要一个类似于ISO格式的字符串,该字符串类似于:2007-08-20T00:00:00Z.似乎SharePoint仅接受不包含毫秒的UTC日期时间(无论出于何种原因,SharePoint都会出错并且当包含毫秒时不接受DateTime),因此我需要将本地时间转换为UTC时间,然后再将毫秒转换为UTC时间.将其转换为ISO字符串.

Right now I'm trying to parse a date that's written in a human-readable format into a DateTime String that a SharePoint list will accept. In order to do this I've determined that I need a String in a format similar to ISO that looks like: 2007-08-20T00:00:00Z. It seems that SharePoint only accepts DateTimes that are in UTC with no milliseconds included (for whatever reason, SharePoint gives errors and won't accept the DateTime when you include the milliseconds), so I need to convert my local time into a UTC time before converting it to the ISO string.

这是下面的代码正在使用的过程.

Here's the process that the code below is using.

  1. 首先,我使用DateJS将人类日期解析为JavaScript日期. (工作正常,但显然DateJS已被放弃,所以也许我 应该将其更改为使用MomentJS.)
  2. 接下来,我尝试创建一个新的 UTC的时刻. (此行非常非常错误,导致我的 程序.)
  3. 然后我有SPServices将其转换为ISO. SPServices将从DateTime减去毫秒,以便SharePoint接受它. (作品 很好).
  1. First I use DateJS to parse my human-date into a JavaScript Date. (Works fine, but apparently DateJS has been abandoned, so maybe I should change this to use MomentJS.)
  2. Next I tried to create a new moment in UTC. (This line is very, very wrong, and crashes my program.)
  3. Then I have SPServices convert it into an ISO. SPServices drops the milliseconds off the DateTime so that SharePoint will accept it. (Works fine).

我确信必须有一种更优雅/更有效的方法来实现这一目标,而不是将3个不同的库拼接在一起.我只是不确定是什么.

I'm sure there has to be a more elegant/working way to achieve this, instead of stitching together 3 different libraries. I'm just not sure what it is.

var jScriptStartDate = Date.parse("6/29/2014 8:30am"); //JS Date
var jScriptStartDateUTC = moment(jScriptStartDate).utc(); //local date to UTC.
var startDate = $().SPServices.SPConvertDateToISO({ //Sharepoint ISO 8601 format
   dateToConvert: jScriptStartDateUTC,
   dateOffset: "" //Sharepoint dates only accept UTC times, aka no dateOffset.
});
newItem.set_item('EventDate', startDate); //EventDate is internal for StartTime

推荐答案

您可以只使用moment.js,而这一切都在文档.

You can just use moment.js, and this is all in the documentation.

moment('6/29/2014 8:30am','M/D/YYYY h:mma').toISOString()

这假定所有以下条件:

  • 源值位于用户的时区(即-运行JavaScript代码的计算机所在的时区)

  • The source value is in the user's time zone (that is - the time zone of the machine where the JavaScript code is running)

输入将始终采用指定的格式

The input will always be in the specified format

还值得一提的是,如果您在"am"之前放置一个空格,则大多数现代浏览器都可以在没有任何库的情况下以本机方式实现:

It's also worth mentioning that if you put a space before the "am", that most modern browsers can do this natively without any library:

new Date('6/29/2014 8:30 am').toISOString()

如果采用这种方法,请意识到日期部分是根据用户区域设置排序的,可能是m/d/y,d/m/y或y/m/d.

If you take that approach, realize that the date parts are ordered according to the users locale, which might be m/d/y, or d/m/y or y/m/d.

此外,您在标题"...中没有毫秒"中说过,但没有在您的问题中进行详细说明.我相当确定您可以顺利通过毫秒.没有充分的理由不遗余力地将其删除.但是,如果必须的话,那会儿像这样:

Also, you said in the title "... with no milliseconds", but didn't elaborate on that in your question. I'm fairly certain you can pass the milliseconds without issue. There's no good reason to go out of your way to remove them. But if you must, then that would be like this with moment:

moment('6/29/2014 8:30am','M/D/YYYY h:mma').utc().format('YYYY-MM-DD[T]HH:mm:ss[Z]')

这篇关于将JavaScript DateTime转换为UTC,无需毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆