jquery trim leading zeros from date of format 01/02/2010

心已入冬 提交于 2019-12-08 02:09:34

问题


How do I trim javascript date object returned as 01/26/2012 into 1/26/2012? it could be applicable for both month or days. So 01/01/2012 should be trimmed as 1/1/2012. Regular expression? jquery trim function? I am not sure how to go on this?

var date=date.replace(/^0+/, ''); 

or

var trimmed = s.replace(/\b(0(?!\b))+/g, "") 

回答1:


For a simple string operation, a RegEx can be used:

date = date.replace(/\b0(?=\d)/g, '')



回答2:


If it is indeed a date object format it.

Quick example (See it in action):

var today = new Date();
var today_string = (today.getMonth() + 1) + '/' + today.getDate() + '/' + today.getFullYear();
alert(today_string);

Either way, this is not a good use of regular expressions.




回答3:


Convert to a Date object first. http://www.w3schools.com/jsref/jsref_obj_date.asp

Javascript doesn't have built-in formatting functions for dates, but there are a lot of simple libraries that serve this need. Personal favorite: http://blog.stevenlevithan.com/archives/date-time-format



来源:https://stackoverflow.com/questions/8913580/jquery-trim-leading-zeros-from-date-of-format-01-02-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!