jQuery 日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5250244/
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-26 18:49:07  来源:igfitidea点击:

jQuery date formatting

jquery

提问by DotnetSparrow

How can I format the date using jQuery. I am using below code but getting error:

如何使用 jQuery 格式化日期。我正在使用以下代码但出现错误:

 $("#txtDate").val($.format.date(new Date(), 'dd M yy'));

Please suggest a solution.

请提出解决方案。

采纳答案by Pekka

jQuery dateFormatis a separate plugin. You need to load that explicitly using a <script>tag.

jQuery dateFormat是一个单独的插件。您需要使用<script>标签显式加载它。

回答by Thulasiram

add jquery ui plugin in your page.

在您的页面中添加 jquery ui 插件。

 $("#txtDate").val($.datepicker.formatDate('dd M yy', new Date()));

回答by Pascal

An alternative would be simple js date() function, if you don't want to use jQuery/jQuery plugin:

如果您不想使用 jQuery/jQuery 插件,另一种选择是简单的 js date() 函数:

e.g.:

例如:

var formattedDate = new Date("yourUnformattedOriginalDate");
var d = formattedDate.getDate();
var m =  formattedDate.getMonth();
m += 1;  // JavaScript months are 0-11
var y = formattedDate.getFullYear();

$("#txtDate").val(d + "." + m + "." + y);

see: 10 ways to format time and date using JavaScript

请参阅:使用 JavaScript 格式化时间和日期的 10 种方法

If you want to add leading zeros to day/month, this is a perfect example: Javascript add leading zeroes to date

如果你想给日/月添加前导零,这是一个完美的例子: Javascript addleading zeroes to date

and if you want to add time with leading zeros try this: getMinutes() 0-9 - how to with two numbers?

如果你想用前导零添加时间,试试这个: getMinutes() 0-9 - 如何用两个数字?

回答by Owen

Here's a really basic function I just made that doesn't require any external plugins:

这是我刚刚制作的一个非常基本的功能,不需要任何外部插件:

$.date = function(dateObject) {
    var d = new Date(dateObject);
    var day = d.getDate();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    if (day < 10) {
        day = "0" + day;
    }
    if (month < 10) {
        month = "0" + month;
    }
    var date = day + "/" + month + "/" + year;

    return date;
};

Use:

用:

$.date(yourDateObject);

Result:

结果:

dd/mm/yyyy

回答by Dominik Ras

ThulasiRam, I prefer your suggestion. It works well for me in a slightly different syntax/context:

ThulasiRam,我更喜欢你的建议。它在稍微不同的语法/上下文中对我来说效果很好:

var dt_to = $.datepicker.formatDate('yy-mm-dd', new Date());

If you decide to utilize datepicker from JQuery UI, make sure you use proper references in your document's < head > section:

如果您决定使用JQuery UI 中的datepicker ,请确保在文档的 <head> 部分使用正确的引用:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />

<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> 

回答by Jhonatascf

I'm using Moment JS. Is very helpful and easy to use.

我正在使用Moment JS。非常有用且易于使用。

var date = moment(); //Get the current date
date.format("YYYY-MM-DD"); //2014-07-10

回答by Sein Navarro

I hope this code will fix your problem.

我希望这段代码能解决你的问题。

var d = new Date();

var curr_day = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();

var curr_hour = d.getHours();
var curr_min = d.getMinutes();
var curr_sec = d.getSeconds();

curr_month++ ; // In js, first month is 0, not 1
year_2d = curr_year.toString().substring(2, 4)

$("#txtDate").val(curr_day + " " + curr_month + " " + year_2d)

回答by Nipple

Just use this:

只需使用这个:

var date_str=('0'+date.getDate()).substr(-2,2)+' '+('0'+date.getMonth()).substr(-2,2)+' '+('0'+date.getFullYear()).substr(-2,2);

回答by Nipple

Add this function to your <script></script>and call from where ever you want in that <script></script>

将此功能添加到您的<script></script>并从您想要的任何地方调用<script></script>

<script>

function GetNow(){
    var currentdate = new Date(); 
    var datetime = currentdate.getDate() + "-"
            + (currentdate.getMonth()+1)  + "-" 
            + currentdate.getFullYear() + " "  
            + currentdate.getHours() + ":"  
            + currentdate.getMinutes() + ":" 
            + currentdate.getSeconds();
    return datetime;
}

window.alert(GetNow());

</script>

or you may simply use the Jquery which provides formatting facilities also:-

或者您也可以简单地使用提供格式化功能的 Jquery:-

window.alert(Date.parse(new Date()).toString('yyyy-MM-dd H:i:s'));

I love the second option. It resolves all issues in one go.

我喜欢第二种选择。一口气解决所有问题。

回答by Jatin C

If you are using jquery ui then you may use it like below, you can specify your own date format

如果您使用的是 jquery ui 那么您可以像下面这样使用它,您可以指定自己的日期格式

$.datepicker.formatDate( "D dd-M-yy", new Date()) // Output "Fri 08-Sep-2017"