Javascript 你如何用javascript设置strftime?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31089749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:11:40  来源:igfitidea点击:

How do you set a strftime with javascript?

javascriptdatetimestrftime

提问by williamcodes

I need to custom format dates. In ruby, I would use strftime, or string formatted time, to accomplish this.

我需要自定义格式日期。在 ruby​​ 中,我会使用 strftime 或字符串格式化时间来完成此操作。

now = Time.new
now.strftime '%a, %d of %b' #=> "Sat, 27 of Jun"

Does javascript use strftime or some equivalent? How can I get a similar effect in javascript?

javascript 是否使用 strftime 或一些等效的?如何在 javascript 中获得类似的效果?

采纳答案by Walter Chapilliquen - wZVanG

UPDATE

更新

Arguments of toLocaleString method can also configure the format of the dates. This is supported in modern browser versions, you can see more information here.

toLocaleString 方法的参数也可以配置日期的格式。这在现代浏览器版本中受支持,您可以在此处查看更多信息。

let date = new Date(Date.UTC(2015, 5, 27, 12, 0, 0))
, options = {weekday: 'short', month: 'short', day: 'numeric' };
console.log(date.toLocaleString('es-ES', options)); //sáb. 27 de jun.

In JavaScript there are methods to create dates, but no native code to format. You can read about Date(). But there are librariesthat do. Particularly for me the best library to use it deeply is MomentJS. So you can do something like as: moment().format('dd, d of MMMM')

在 JavaScript 中有创建日期的方法,但没有用于格式化的本机代码。您可以阅读有关Date() 的信息。但是有些图书馆可以做到。特别是对我来说,深入使用它的最佳库是MomentJS。因此,您可以执行以下操作:moment().format('dd, d of MMMM')

However, if you do not want to use a library you have access to the following native Date properties:

但是,如果您不想使用库,则可以访问以下本机日期属性:

var now = new Date();

document.write(now.toUTCString() + "<br>")
document.write(now.toTimeString() + "<br>")

Date Objectsome properties

日期对象的一些属性

toDateString()  Converts the date portion of a Date object into a readable string
toGMTString()   Deprecated. Use the toUTCString() method instead
toISOString()   Returns the date as a string, using the ISO standard
toJSON()    Returns the date as a string, formatted as a JSON date
toLocaleDateString()    Returns the date portion of a Date object as a string, using locale conventions
toLocaleTimeString()    Returns the time portion of a Date object as a string, using locale conventions
toLocaleString()    Converts a Date object to a string, using locale conventions
toString()  Converts a Date object to a string
toTimeString()  Converts the time portion of a Date object to a string
toUTCString() Converts a Date object to a string, according to universal time

回答by Prasad Pilla

There are a couple of good strftime() ports for javascript.

javascript 有几个很好的 strftime() 端口。

https://github.com/samsonjs/strftimeis very comprehensive and a lot of specifiers from C-lang and Ruby have been ported.

https://github.com/samsonjs/strftime非常全面,并且移植了很多来自 C-lang 和 Ruby 的说明符。

https://thdoan.github.io/strftime/is simpler if you're looking for something lite.

如果您正在寻找精简版,https://thdoan.github.io/strftime/会更简单。

I appreciate Walter's very detailed and clear answer about custom implementation, but I personally wouldn't want to write code on my own for common things like date formatting(I actually came back to using libs above after seeing repeated issues with custom code).

我很欣赏 Walter 关于自定义实现的非常详细和清晰的回答,但我个人不想为诸如日期格式之类的常见事情自己编写代码(在看到自定义代码的重复问题后,我实际上又回到了使用上面的库)。