[英]Changing the Date Format in Jquery
In javascript, I'm getting the Date & Time as 1/05/2013 20:00
(d/mm/yyyy HH:MM) 在javascript中,我将日期和时间设为
1/05/2013 20:00
(d / mm / yyyy HH:MM)
But I want the date & time in this format 01/05/2013 20:00
(dd/mm/yyyy HH:MM) 但我希望日期和时间采用这种格式
01/05/2013 20:00
(dd / mm / yyyy HH:MM)
jQuery doesn't offer any help with date/time formatting. jQuery不提供有关日期/时间格式的任何帮助。
If you want something sophisticated without much hazzle, take date.js 如果您想要一些复杂而又没有太多麻烦的东西,请选择date.js
However, if adding a "0" to the beginning of the date is everything you ever want, this will be enough: 但是,如果要在日期的开头加上“ 0”就是您想要的一切,那么就足够了:
var date = "1/05/2013 20:00";
function modifyDateAccordingToYourNeeds(date_in)
{
var day_part = date_in.split("/")[0];
if (day_part.length == 1)
date_in = "0" + date_in;
return date_in;
}
alert(modifyDateAccordingToYourNeeds(date));
However, be careful as this approach is very limited. 但是,请注意,因为这种方法非常有限。
maybe use this 也许用这个
var curr_date = date.getDate();
var curr_month = date.getMonth();
var curr_year = date.getFullYear();
date= curr_date + '/'+ curr_month + '/'+ curr_year;
More possibilities 更多可能性
Javascript Java脚本
var d = "1/05/2013 20:00";
function padDay(date) {
if (date.charAt(1) === "/") {
date = "0" + date;
}
return date;
}
console.log(padDay(d));
Javascript Java脚本
var d = "1/05/2013 20:00";
function padDay(date) {
return moment(d, "D/M/YYYY").format("DD/MM/YYYY");
}
console.log(padDay(d));
The Globalize library allows you to do both globalization and customization of dates easily. Globalize库使您可以轻松地对日期进行全球化和自定义。 The following example is taken from the documentation page :
以下示例摘自文档页面 :
Globalize.format( new Date(1955,10,5), "dddd MMMM d, yyyy" ); // "Saturday November 5, 1955"
The globalize library also allows you to output dates in a format appropriate for the culture specified. 全球化库还允许您以适合于指定区域性的格式输出日期。 It supports virtually all available cultures.
它支持几乎所有可用的文化。 Another example:
另一个例子:
// assuming a culture with "/" as the date separator symbol
Globalize.format( new Date(1955,10,5), "yyyy/MM/dd" ); // "1955/11/05"
If you want to do any more globalization, the Globalize library also allows for number and currency globalization. 如果要进行更多的全球化,Globalize库还允许数字和货币全球化。 I have created a small sample project that shows how to use this library for client-side globalization: https://github.com/ErikSchierboom/clientsideglobalization
我创建了一个小示例项目,展示了如何使用此库进行客户端全球化: https : //github.com/ErikSchierboom/clientsideglobalization
Try this: 尝试这个:
function changeFormat(string) {
var p = string; //"1/05/2013 20:00"
var n = /^[0-9]*/.exec(p)[0];
p = p.replace(n, "");
if (n.length == 1) {
n = "0" + n;
}
return n + p;
}
alert(changeFormat("1/05/2013 20:00"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.