javascript - jquery trim leading zeros from date of format 01022010 - Stack Overflow

How do I trim javascript date object returned as 01262012 into 1262012? it could be applicable for

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, "") 

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, "") 
Share Improve this question asked Jan 18, 2012 at 16:21 Anjana SharmaAnjana Sharma 4,7955 gold badges40 silver badges51 bronze badges 1
  • 2 urgh - most people want the reverse :( – Alnitak Commented Jan 18, 2012 at 16:24
Add a ment  | 

3 Answers 3

Reset to default 6

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

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

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.

Convert to a Date object first. http://www.w3schools./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./archives/date-time-format

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744246722a4564967.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信