JS一些常用的日期操作

福尔摩斯对我笑 2019-05-18 44668 次 7362条

日期格式化

Date.prototype.FormatDate = function(dt){
    if( !dt){
      dt = new Date();
    }
    var year = dt.getFullYear();
    var month = dt.getMonth() + 1;
    var day = dt.getDate();
    if (month <= 10) {
      month = '0' + month;
    }

    if (day <= 10) {
      day = '0' + day;
    }
    return year + '-' + month + '-' + day;
}

 

获取当前时间对应的前X个月份


Date.prototype.GetCurrentBeforeTime = function(num){
    num =  !num ? 12: num; 
    var result = [];
    var date = new Date( new Date().setMonth(new Date().getMonth() + 1) );
	for(let i=0; i< num; i++){
		date.setMonth( date.getMonth() - 1) 
		let m = date.getMonth() +1 
		m = m < 10 ? "0" + m : m;
		result.push( date.getFullYear() + "-" + m + "-"  + date.getDate())
    }

    return result;
}

获取当前时间对应的英文格式月份

Date.prototype.GetCurrentEnMonth = function(times){
    //没有参数时返回当前月份
    if(!times || !(times instanceof Array) ){
        return new Date().toDateString().split(" ")[1];
    }

    let monthStr = [];
    times.forEach(elem=>{
		let month = new Date(elem).toDateString().split(" ")[1];
		monthStr.push( month)
    })
    
    return monthStr;
}
标签:

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 7362 条评论 1


  1. assemblers incorporated

    I went over this web site and I conceive you have a lot of fantastic info, saved to fav (:.

    回复 2019年12月6日
  2. logo

    You can definitely see your enthusiasm in the work you write. The arena hopes for more passionate writers like you who aren’t afraid to say how they believe. All the time go after your heart.

    回复 2019年12月6日
  3. udaipur escorts

    Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your weblog? My blog site is in the exact same area of interest as yours and my visitors would certainly benefit from some of the information you provide here. Please let me know if this alright with you. Thanks!

    回复 2019年12月6日
  4. togel online

    Hmm is anyone else experiencing problems with the pictures on this blog loading? I’m trying to determine if its a problem on my end or if it’s the blog. Any responses would be greatly appreciated.

    回复 2019年12月5日
  5. i99bet

    After going over a handful of the blog articles on your web site, I seriously appreciate your way of blogging. I saved it to my bookmark webpage list and will be checking back soon. Please check out my website as well and let me know what you think.

    回复 2019年12月4日
  6. John Deere Repair Manuals

    There are some interesting deadlines on this article but I don’t know if I see all of them middle to heart. There is some validity but I’ll take maintain opinion till I look into it further. Good article , thanks and we wish extra! Added to FeedBurner as well

    回复 2019年12月3日
  7. i99bet

    I really like it when folks get together and share thoughts. Great website, continue the good work!

    回复 2019年12月3日
  8. Pozycjonowanie Cennika Gorzow Wielkopolski

    I like what you guys are up too. Such smart work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my website :).

    回复 2019年11月30日
  9. Hulda Wela

    I need yet to really understand together with comprehend that essence in the contents from your article

    回复 2019年11月29日
  10. Demarcus Delley

    I am not sure where you’re getting your information, but great topic. I needs to spend some time learning much more or understanding more. Thanks for great info I was looking for this information for my mission.

    回复 2019年11月28日
  11. view publisher site

    I just want to say I’m beginner to blogging and truly loved you’re web page. More than likely I’m likely to bookmark your site . You amazingly have incredible writings. Cheers for sharing with us your web page.

    回复 2019年11月24日
  12. StevDede

    Cialis Effetti Indesiderati viagra online pharmacy Propecia Espalnds Bueno

    回复 2019年11月5日