将以下日期格式化为JS中的YYYY-mm-dd [英] Format the following date into YYYY-mm-dd in JS

查看:948
本文介绍了将以下日期格式化为JS中的YYYY-mm-dd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换以下日期:

Thu Feb 18 12:25:00 SGT 2016

Thu Feb 18 12:25:00 SGT 2016

转换成2的格式?

我知道,


  1. 使用新的Date(Date.parse ('...')),随着电话,将帮助我得到它。但问题是时区部分(SGT)。

  2. 我不想使用任何图书馆。

什么是有效的方式要解决这个问题?

What would be the effective way to go about this?

任何帮助不胜感激!

PS:

好的,我试过

new Date(Date.parse('Thu Feb 18 12:25:00 SGT 2016'))

但当然,这将是我无效的日期错误。

but of-course, it is going to me 'invalid date' error.

推荐答案

我想我应该等待你发布一些代码,但这里是一个例子。它将字符串分割成多个部分,将月份名称转换为一个数字,然后按照您想要的顺序输出所需的位。月份名称上的切片仅供您使用完整的月份名称,但也许这是不必要的:

I guess I should wait for you to post some code, but here's an example. It splits the string into parts, converts the month name to a number, then outputs the bits you want in the order you want. The slice on the month name is just in case you want to use the full month name, but maybe that's unnecessary:

function reformatDate(s){
  var b = s.split(/[\s:]/);
  var months = {jan:'01',feb:'02',mar:'03',apr:'04',may:'05',jun:'06',
                jul:'07',aug:'08',sep:'09',oct:'10',nov:'11',dec:'12'};
  return b[7] + '/' + months[b[1].toLowerCase().slice(0,3)] + '/' + ('0'+b[2]).slice(-2);
}

document.write(reformatDate('Thu Feb 18 12:25:00 SGT 2016'));

格式日期字符串(y / m / d)与我所知道的任何标准不一致,大多数浏览器可能无法解析。

That format date string (y/m/d) isn't consistent with any standard that I know of and likely will not be parsed by most browsers.

这篇关于将以下日期格式化为JS中的YYYY-mm-dd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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