最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - date format javascript - Stack Overflow

programmeradmin3浏览0评论

The script below returns dates in this format:

Fri Apr 21 2011 12:18:25 GMT+0200

Fri Apr 22 2011 12:18:25 GMT+0200

Fri Apr 29 2011 12:18:25 GMT+0200

My question is: how to get dates in this format:

dd/mm/year like so: 21/04/2001

function addDays(dateObj, numDays) {
   dateObj.setDate(dateObj.getDate() + numDays);
   return dateObj;
}
var now = new Date();
var tomorrow = addDays(new Date(), 1);
var nextWeek = addDays(new Date(), 8);

    {
jQuery("input[id*='now']").val(now);
jQuery("input[id*='tomorrow']").val(tomorrow);
jQuery("input[id*='nextweek']").val(nextWeek);
    }

The script below returns dates in this format:

Fri Apr 21 2011 12:18:25 GMT+0200

Fri Apr 22 2011 12:18:25 GMT+0200

Fri Apr 29 2011 12:18:25 GMT+0200

My question is: how to get dates in this format:

dd/mm/year like so: 21/04/2001

function addDays(dateObj, numDays) {
   dateObj.setDate(dateObj.getDate() + numDays);
   return dateObj;
}
var now = new Date();
var tomorrow = addDays(new Date(), 1);
var nextWeek = addDays(new Date(), 8);

    {
jQuery("input[id*='now']").val(now);
jQuery("input[id*='tomorrow']").val(tomorrow);
jQuery("input[id*='nextweek']").val(nextWeek);
    }
Share Improve this question asked Apr 21, 2011 at 10:26 user472285user472285 2,6745 gold badges38 silver badges55 bronze badges 1
  • take a look at : cev.washington.edu/lc/CLWEBCLB/jst/js_datetime.html – xkeshav Commented Apr 21, 2011 at 10:32
Add a ment  | 

8 Answers 8

Reset to default 3
var date = new Date();
var d  = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;

alert(day + "/" + month + "/" + year);

Try this.

I suggest you can use Steve Levithan's dateformat script: http://blog.stevenlevithan./archives/date-time-format

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_date + "-" + curr_month + "-" + curr_year);

You just may wrap it or use one of the existing helpers

Check out moment.js! It's a really powerful little library for working with Dates in JavaScript.

var today = moment(new Date());
today.format("D/M/YYYY");                  // "4/11/2012"
today.format("MMMM D, YYYY h:m A");        // outputs "April 11, 2012 2:42 PM"

// in one line...
moment().format("D/M/YYYY");               // "4/11/2012"
moment().format("MMMM D, YYYY h:m A");     // outputs "April 11, 2012 2:42 PM"

Another example...

var a = moment([2012, 2, 12, 15, 25, 50, 125]);
a.format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Monday, March 12th 2012, 3:25:50 pm"
a.format("ddd, hA");                       // "Mon, 3PM"

Also, its worth mentioning to checkout date.js. I think the two libraries plement each other.

The formatting library https://github./anywhichway/stringformatter will address this issue and more. It supports most of the momentjs formats plus U, G, I, L, T, to return UTC, GTM, ISO, Local, and Time strings respectively. It also provides a consistent approach that can be used for formatting other things.

Format expressions using this library take the form of embedded Javascript objects, e.g.

format("{Date: {format: 'DD-MMM-YYYY hh:mm:ss:SSS A'}}",new Date('2015-04-12 00:12:23'));

will return

"12-Apr-2015 12:12:23:000 AM"

Using plain old JavaScript, something like this should work:

var formatDateString = function(s) {
  var pad = function(x) { return ((x.length<2) ? "0" : "") + x; }
    , dt = new Date("" + s), d, m, y;
  if (dt.getTime()) {
    d = pad(dt.getDate());
    m = pad(dt.getMonth()+1);
    y = dt.getFullYear();
    return [d, m, y].join('/');
  }
  return null;
};
formatDateString("Fri Apr 21 2011 12:18:25 GMT+0200"); // => "21/04/2011"

If you're already using jquery UI and the date picker

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

In pure Javascript:

function dmy(d) {

    function pad2(n) {
        return (n < 10) ? '0' + n : n;
    }

    return pad2(d.getUTCDate()) + '/' + 
           pad2(d.getUTCMonth() + 1) + '/' +
           d.getUTCFullYear();
}
发布评论

评论列表(0)

  1. 暂无评论