javascript将日期和时间转换为特定格式
javascript convert date and time into specific format
我是个新手,想把2014-09-25T09:54:00
转换成9:54 AM Thu, 25th Sep
。。
有人能给我一个用javascript实现的方法吗。
感谢
here is a great JavaScript library that handles this very well, and only 5.5kb minified.
http://momentjs.com/
It looks something like this:
moment().format('MMMM Do YYYY, h:mm:ss a'); // February 25th 2013, 9:54:04 am
moment().subtract('days', 6).calendar(); // "last Tuesday at 9:53 AM"
You can also pass in dates as a String with a format, or a Date object.
var date = new Date();
moment(date); // same as calling moment() with no args
// Passing in a string date
moment("12-25-1995", "MM-DD-YYYY");
Also has great support for languages other then English, like, Russian, Japanese, Arabic, Spanish, etc..
我为你的案子做了一把小提琴。
它基于这个很棒的教程,它使格式化日期变得非常容易。
Javascript代码:
var d_names = new Array("Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat");
var m_names = new Array("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sept",
"Oct", "Nov", "Dec");
var d = new Date();
var curr_date = d.getDate();
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
{
sup = "st";
}
else if (curr_date == 2 || curr_date == 22)
{
sup = "nd";
}
else if (curr_date == 3 || curr_date == 23)
{
sup = "rd";
}
else
{
sup = "th";
}
var a_p = "";
var curr_hour = d.getHours();
if (curr_hour < 12)
{
a_p = "AM";
}
else
{
a_p = "PM";
}
if (curr_hour == 0)
{
curr_hour = 12;
}
if (curr_hour > 12)
{
curr_hour = curr_hour - 12;
}
var curr_day = d.getDay();
var curr_min = d.getMinutes();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
alert((curr_hour + " : " + curr_min + " " + a_p)+ (" " + d_names[curr_day] + ", " + curr_date + sup + " "
+ m_names[curr_month] /* +" " + curr_year */));
正如Phrogz在这里所说:
- 将字符串解析为JavaScript
Date
对象- 根据需要设置Date对象的格式
另请参阅:为什么Date.parse给出错误的结果?以及如何格式化JavaScript日期问题
示例:
var myDate = new Date("2014-09-25T09:54:00")
var myString = myDate.customFormat("#h#:#mm# #AMPM# #DDD#, #D##th# #MMM#");
请在此处查看此代码的作用
相关文章:
- 如何使用javascript或html下载PDF格式的填写表单
- 货币代码为欧元-金额的格式不应包含小数
- Json数据包含日期和时间格式
- Javascript日期格式类似于ISO,但本地
- 更改angularjs中的日期-时间格式
- 将日期时间从json转换为可读格式
- NodeJS日期格式不起作用
- 我能得到正确的格式吗
- PHP:显示sqlite数据库中的html格式数据,使用tinymce保存
- 使用正则表达式将输入格式设置为单字符逗号、单字符逗号等
- 将日期和时间转换为UTC格式的日期-Javascript
- Javascript格式UTC日期
- 元素的内容必须由格式正确的字符数据或标记组成
- Datetime格式为Friendly Time.Moment JS输出错误
- 将客户端特定的日期格式返回到服务器MVC4
- 将数字四舍五入到小数点后两位,以设置货币格式
- javascript/jquery将utc转换为短格式的本地时间
- JavaScript生成html格式的密码
- ui网格日期单元格过滤器,过滤日期格式导致显示错误的日期
- 如何通过数据分组将json数据转换为嵌套的json数据格式