Formatting the Date in JavaScript

前端 未结 6 1625
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 06:54

Using newDate() function in Java script, I am able to get today\'s date. I am getting the date in the format 3/3/2009 (d/m/yyyy). But i actually need the date in the format

相关标签:
6条回答
  • 2021-01-16 07:09

    You usually have to write your own function to handle the formatting of the date as javascript doesn't include nice methods to format dates in user defined ways. You can find some nice pieces of code on the net as this has been done to death, try this:

    http://blog.stevenlevithan.com/archives/date-time-format

    Edit: The above code seems to be really nice, and installs a cool 'format' method via the date object's prototype. I would use that one.

    0 讨论(0)
  • 2021-01-16 07:17

    There's a very nice library to manage date in JS.

    Try this.

    0 讨论(0)
  • 2021-01-16 07:23

    You'll pretty much have to format it yourself, yeah.

    var curDate = new Date();
    var year = curDate.getFullYear();
    var month = curDate.getMonth() + 1;
    var date = curDate.getDate();
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    var dateString = year + "-" + month + "-" + date;
    

    It's a bit long, but it'll work (:

    0 讨论(0)
  • 2021-01-16 07:23

    Just another option, which I wrote:

    DP_DateExtensions Library

    Not sure if it'll help, but I've found it useful in several projects.

    Supports date/time formatting, date math (add/subtract date parts), date compare, date parsing, etc. It's liberally open sourced.

    No reason to consider it if you're already using a framework (they're all capable), but if you just need to quickly add date manipulation to a project give it a chance.

    0 讨论(0)
  • 2021-01-16 07:25

    add jquery ui plugin in your page.

    function DateFormate(dateFormate, dateTime) {
        return $.datepicker.formatDate(dateFormate, dateTime);
    };
    
    0 讨论(0)
  • 2021-01-16 07:26

    If you want to roll-your-own, which is not too difficult, you can use the built-in javascript Date Object methods.

    For example, to get the current date in the format you want, you could do:

    var myDate = new Date();
    var dateStr = myDate.getFullYear + 
        '-' + (myDate.getMonth()+1) + '-' + myDate.getDate();
    

    You may need to zero-pad the getDate() method if you require the two-digit format on the day.

    I create a few useful js functions for date conversions and use those in my applications.

    0 讨论(0)
提交回复
热议问题