如何使用javascript设置strftime? [英] How do you set a strftime with javascript?

查看:56
本文介绍了如何使用javascript设置strftime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

推荐答案

更新

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.

在JavaScript中,有一些方法可以创建日期,但没有格式的本机代码.您可以阅读 Date()> .但是,有.特别是对我而言,最好深入使用它的库是 MomentJS .因此,您可以执行以下操作: moment().format('dd,MMMM的d')

In JavaScript there are methods to create dates, but no native code to format. You can read about Date(). But there are libraries that 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')

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

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>")

日期对象一些属性

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

这篇关于如何使用javascript设置strftime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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