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

Javascript date formatting Fri Jun 21 00:00:00 UTC+0100 2013 to iso 8601 (2013-06-21) - Stack Overflow

programmeradmin5浏览0评论

Please advise:

Is there a way without regex or string replaces and so forth to convert a simple date such as:

Fri Jun 21 00:00:00 UTC+0100 2013

To a ISO8601 formatted date yy-mm-dd?

PS: 8601 date only, not date time.

Please advise:

Is there a way without regex or string replaces and so forth to convert a simple date such as:

Fri Jun 21 00:00:00 UTC+0100 2013

To a ISO8601 formatted date yy-mm-dd?

PS: 8601 date only, not date time.

Share Improve this question edited Jan 1, 2017 at 16:42 Cœur 38.7k26 gold badges203 silver badges277 bronze badges asked Jun 4, 2013 at 13:14 Joshua HoldenJoshua Holden 3701 gold badge4 silver badges14 bronze badges 6
  • 2 Without regular expressions or string replacement? What would you like to use instead? – Sean Bright Commented Jun 4, 2013 at 13:15
  • I wondered if there was a C# esque format... albeit at this point any method will do, i'm getting fed up of trying numerous regex replaces of which i'm not very good with! – Joshua Holden Commented Jun 4, 2013 at 13:17
  • 3 Is your starting date (Fri Jun 21 00:00:00 UTC+0100 2013) just a string or is it a Date object? – Sean Bright Commented Jun 4, 2013 at 13:19
  • 2 As an aside, that's a weird input format: I don't recall ever (before) seeing a date/time format that puts the time part in the middle of the date parts. – nnnnnn Commented Jun 4, 2013 at 13:22
  • 1 It's just a string at this point, i'm using knockout and trying to write a custom binding... and failing at this part :P The string is ing from select event: datepicker (jquyeryUI) – Joshua Holden Commented Jun 4, 2013 at 13:23
 |  Show 1 more ment

4 Answers 4

Reset to default 5

Use moment.js http://momentjs./

moment(new Date(), "YYYY-MM-DD HH:mm Z");

or:

var date = moment("Fri Jun 21 00:00:00 UTC+0100 2013");
moment("Fri Jun 21 00:00:00 UTC+0100 2013", "YYYY-MM-DD HH:mm Z");

You can parse it and format it very easily whatever way you want http://momentjs./docs/ it is patible with ISO-8601 dates for parsing as well.

Yes !

the date function in javascript.

var d = new Date("Fri Jun 21 00:00:00 UTC+0100 2013")
alert( d.getFullYear() + '-' + d.getUTCMonth() + '-' + d.getUTCDay())

2 lines of code :)

more info here : http://www.w3schools./jsref/jsref_obj_date.asp

Without regexes or string replaces? Yes, assuming that the format is fixed you could use .slice() and/or .substr() to extract the particular bits you need and rearrange them (unless such methods fall into your category of "and so forth"):

var input = "Fri Jun 21 00:00:00 UTC+0100 2013";

var year = input.slice(-4),
    month = ['Jan','Feb','Mar','Apr','May','Jun',
             'Jul','Aug','Sep','Oct','Nov','Dec'].indexOf(input.substr(4,3))+1,
    day = input.substr(8,2);

var output = year + '-' + (month<10?'0':'') + month + '-' + day;

Or you could go ahead and get silly with a regex replace:

var output = input.replace(/^[^\s]+\s([^\s]+)\s(\d+)\s.*(\d{4})$/,function(m,p1,p2,p3) {
   var month = ['Jan','Feb','Mar','Apr','May','Jun',
                'Jul','Aug','Sep','Oct','Nov','Dec'].indexOf(p1)+1;
   return p3 + '-' + (month<10?'0':'') + month + '-' + (p2.length===1?'0':'') + p2;
});

Of course you'd probably want to wrap such code in a reformatDate() method.

(For a "simple" reformatting of a date string, the Date object and its methods aren't particularly helpful unless the starting format is one recognised by Date.parse().)

Why dont you try to use the get functions, like getDate(), getMonth(), etc. For example:

var today = new Date();
var d1 = new Date();
alert(d1);
var date = d1.getDate();
var month = d1.getMonth() + 1;
var year = d1.getFullYear();

Then configure the string the way you want it to appear...!

发布评论

评论列表(0)

  1. 暂无评论