admin管理员组文章数量:1361136
I've seen this: how to get formatted date time like 2009-05-29 21:55:57 using javascript?
but I do need the 0's before a single digit date(I need 2011-04-01, not 2011-4-1)
I don't like to add another unnecessary plugin for this one so if there's a builtin function or SIMPLE function in js/jquery that could do this, I'm good. I do hope it will not result to a 10 line function just to check if the month was singular and then add a zero to it.
Basically, I am using the AnyTime js (/) plugin but it throws an error if my starting date was not in the form "yyyy-mm-dd". My starting default value is "now" so I am trying to convert a new Date() to that format. Any help?
NOTE please don't give links of plugins as i was hoping for a REALLY simple solution. i find that using a plugin just to fix a plugin isn't actually very clean.
I've seen this: how to get formatted date time like 2009-05-29 21:55:57 using javascript?
but I do need the 0's before a single digit date(I need 2011-04-01, not 2011-4-1)
I don't like to add another unnecessary plugin for this one so if there's a builtin function or SIMPLE function in js/jquery that could do this, I'm good. I do hope it will not result to a 10 line function just to check if the month was singular and then add a zero to it.
Basically, I am using the AnyTime js (http://www.ama3./anytime/) plugin but it throws an error if my starting date was not in the form "yyyy-mm-dd". My starting default value is "now" so I am trying to convert a new Date() to that format. Any help?
NOTE please don't give links of plugins as i was hoping for a REALLY simple solution. i find that using a plugin just to fix a plugin isn't actually very clean.
Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Apr 1, 2011 at 11:04 corrodedcorroded 21.6k19 gold badges86 silver badges133 bronze badges6 Answers
Reset to default 3 function getFormattedDate() {
var date = new Date();
var str = date.getFullYear() + "-" + getFormattedPartTime(date.getMonth()) + "-" + getFormattedPartTime(date.getDate()) + " " + getFormattedPartTime(date.getHours()) + ":" + getFormattedPartTime(date.getMinutes()) + ":" + getFormattedPartTime(date.getSeconds());
return str;
}
function getFormattedPartTime(partTime){
if (partTime<10)
return "0"+partTime;
return partTime;
}
vvk's effort is pretty much on the money, here's a minor modification of the same thing:
var getDate = (function() {
function addZ(n) {
return n<10? '0'+n : ''+n;
}
return function() {
var d = new Date();
return d.getFullYear()+'-'+addZ(d.getMonth()+1)+'-'+addZ(d.getDate());
}
}());
The Any+Time(TM) library includes an object named AnyTime.Converter specifically for parsing and formatting dates, so you could have done this:
(new AnyTime.Converter({format:"%Y-%M-%d"})).format(new Date());
However, the library also defaults to the current date/time if the field is empty or does not match the format specifier, so you really could just leave the field empty when you display the form!
If anyone has additional problems or questions, you can usually reach me directly via the links on my website.
also:
Convert javascript to date object to mysql date format (YYYY-MM-DD)
-- pete
I think you are looking for this
This nice code covers all types of formats
use this syntax
var now = new Date();
now.format("mm/dd/yyyy");
本文标签: jqueryget Javascript date formatted as 20110401Stack Overflow
版权声明:本文标题:jquery - get Javascript date formatted as 2011-04-01 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743918816a2561704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论