将dd.mm.yyyy格式转换为yyyy-mm-dd [英] Convert dd.mm.yyyy format to yyyy-mm-dd
问题描述
如何在JavaScript中将dd.mm.yyyy格式日期转换为yyyy-mm-dd格式?
How can I convert dd.mm.yyyy format date to yyyy-mm-dd format in JavaScript?
以下是一个示例:
30.01.2010
to
2010-01-30
意思是将dmY转换为Ymd。我知道如何在PHP中执行此操作,但我需要在JavaScript中使用它。
Meaning convert d.m.Y to Y-m-d. I know how to do this in PHP but I need it in JavaScript.
推荐答案
Datejs 可以解析它。代码位于 http://datejs.googlecode.com/files/date.js
Datejs can parse that. The code is at http://datejs.googlecode.com/files/date.js
编辑:不是安全的,因为date.js自动确定格式字符串。我犯了一个错误,就是没有测试一天< = 12(duh)。您应该使用:
It is not safe to left date.js determine the format string automatically. I made the mistake of not testing with a day <= 12 (duh). You should use:
Date.parseExact('09.01.2010', 'd.M.yyyy').toString('yyyy-MM-dd');
或
Date.parseExact('09.01.2010', 'dd.MM.yyyy').toString('yyyy-MM-dd');
取决于您是否允许单位数天。
depending on whether you want to allow single digit days.
这篇关于将dd.mm.yyyy格式转换为yyyy-mm-dd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!