将具有完整月份名称和时间的日期转换为ISO日期

时间:2018-07-08 18:31:37

标签: javascript date date-conversion datestamp

我有一个日期和时间戳,格式为 Dim oWord As Word.Application Dim oDoc As Word.Document Dim oTable As Word.Table 'Start Word and open the document template. oWord = CreateObject("Word.Application") oDoc = oWord.Documents.Add 'Insert a 3 x 5 table, fill it with data, and make the first row 'bold and italic. Dim r As Integer, c As Integer oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5) oTable.Range.ParagraphFormat.SpaceAfter = 6 For r = 1 To 3 For c = 1 To 5 oTable.Cell(r, c).Range.Text = "שורה " & r & "עמודה " & c Next Next oTable.TableDirection = Word.WdTableDirection.wdTableDirectionRtl oTable.Borders.Enable = 1 oTable.Rows.Item(1).Range.Font.Bold = True oTable.Rows.Item(1).Range.Font.Italic = True oTable.Rows.Item(1).Range.ParagraphFormat.Alignment = _ Word.WdParagraphAlignment.wdAlignParagraphCenter

这是由jQuery选择器生成的:

public Retrofit imageServer() {
      return new Retrofit.Builder()                
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())                
                    .addConverterFactory(GsonConverterFactory.create())               
                    .baseUrl("https://randomuser.me/api"
                    .build();   
}

如何将其转换为02 December 2016 18:00pm格式?

如果可能的话,我想在同一行上进行。否则它需要适应这种结构...

"datestamp": $('span.article_info__published').text(),

(我不记得目标格式的名称了……我认为是ISO 8601?)

更新:以下内容返回“无效日期” ...

2007-07-24T12:50:00+01:00

2 个答案:

答案 0 :(得分:0)

您需要首先解析字符串以获取其组成部分。然后,您可以生成日期并使用其方法来生成合适的字符串,也可以手动重新设置字符串的格式。两种方法非常相似。

我不清楚您为什么要时区偏移。您可以独立获得它,但是如果您仅将所有日期转换为UTC和ISO 8601,则可以采用主机时区偏移量。如果UTC正常,则只需解析为一个Date并使用 toISOString

将时间指定为“ 18:00 pm”是不寻常的,pm部分是多余的。通常将其指定为“ 1800hrs”,“ 18:00”或“ 6:00 pm”。

// Reformat string, using Date object for 
// host timezone offset only
function reformatDate(s) {
  function z(n){return ('0'+n).slice(-2)}
  var b = s.match(/\d+|[a-z]+/gi);
  var months = ['jan','feb','mar','apr','may','jun',
                'jul','aug','sep','oct','nov','dec'];
  var monNum = months.indexOf(b[1].substr(0,3).toLowerCase());
  // Host timezone offset for given date and time
  var tzOffset = new Date(b[2], monNum - 1, b[0], b[3], b[4]).getTimezoneOffset();
  var tzSign = tzOffset > 0? '-' : '+';
  tzOffset = Math.abs(tzOffset);
  return b[2] + '-' +
         z(monNum) + '-' +
         b[0] + 'T' + 
         b[3] + ':' +
         b[4] + tzSign +
         z(tzOffset/60 | 0) + ':' +
         z(tzOffset%60);
}

// Reformat string using Date object for 
// parts and host timezone offset
function reformatDate2(s) {
  function z(n){return ('0'+n).slice(-2)}
  var b = s.match(/\d+|[a-z]+/gi);
  var months = ['jan','feb','mar','apr','may','jun',
                'jul','aug','sep','oct','nov','dec'];
  var monNum = months.indexOf(b[1].substr(0,3).toLowerCase());
  var d = new Date(b[2], monNum - 1, b[0], b[3], b[4]);
  // Host timezone offset for given date and time
  var tzOffset = d.getTimezoneOffset();
  var tzSign = tzOffset > 0? '-' : '+';
  tzOffset = Math.abs(tzOffset);
  return d.getFullYear() + '-' +
         z(d.getMonth() + 1) + '-' +
         z(d.getDate()) + 'T' + 
         z(d.getHours()) + ':' +
         z(d.getMinutes()) + tzSign +
         z(tzOffset/60 | 0) + ':' +
         z(tzOffset%60);
}

var s = '02 December 2016 18:00pm';

console.log(reformatDate(s));
console.log(reformatDate2(s));

如您所见,您实际上仅是使用Date来获取时区偏移,其余的值都可以按原样使用,除了month,在两种情况下都必须转换为数字。 / p>

还有许多库可以帮助解析和格式化字符串,例如moment.js(大型,广泛使用,功能齐全)和fecha.js(小型且功能强大的解析器/格式器)。在这两种情况下,您都可以根据需要解析字符串并设置其格式,例如使用fecha.js:

var s = '02 December 2016 18:00pm';
// Create a Date
var d = fecha.parse(s, 'DD MMMM YYYY HH:mm');
// Format string
console.log(fecha.format(d, 'YYYY-MM-DDTHH:mmZZ'));

解析和格式可以是一条语句,但更清楚地表示为2。使用moment.js(对链接方法有更好的支持):

moment(s, 'DD MMMM YYYY HH:mm').format('YYYY-MM-DDTHH:mmZZ');

答案 1 :(得分:0)

要将该字符串解释为转换日期,我需要先从字符串后面删除时间“ pm”。

var date = "02 December 2016 18:00pm"

date = date.slice(0, -2);

var myDate = new Date(date);

document.write(myDate);
相关问题