使用JavaScript将日期字符串转换为其他格式 [英] Convert dates string to different format with javascript

查看:72
本文介绍了使用JavaScript将日期字符串转换为其他格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在使用Google Calendar API拉动事件的脚本中所拥有的:

This is what I have in a script that is pulling events with a Google Calendar API:

var datestring2 = (startJSDate.getMonth() + 1) + "/" + startJSDate.getDate();

在将其附加到列表后,它以12/2格式打印出来,而我希望将其打印出Friday, Dec 2.

After I append this to a list it prints out in the format 12/2 while I want it to print out Friday, Dec 2.

我该怎么做?我已经调查过date.js但没有运气.

How can I do this? I have looked into date.js but had no luck.

推荐答案

Javascript中没有内置函数可以做到这一点(我想您正在追求类似PHP的date()函数之类的东西).

There is no built in function in Javascript that can do that (I presume you are after something like PHP's date() function).

您当然可以按照其他答案的建议使用自己的解决方案,但是除非您真的反对,否则date.js对此非常有用.

You can certainly roll your own solution as other answers have suggested, but unless you are really against it, date.js is great for this.

您可以使用库toString()函数来获取格式化的日期字符串,如下所示:

You can use the libraries toString() function to get formatted date strings like so:

Date.today().toString("d-MMM-yyyy");

更多信息,请参见 DateJS API文档.

这篇关于使用JavaScript将日期字符串转换为其他格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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