html - How to get the full date with javascript? - Stack Overflow

I am trying to get the full date with my function. Right now my results are "Wed Feb 14 2018"

I am trying to get the full date with my function. Right now my results are "Wed Feb 14 2018", but I would like it to say "Wednesday February 2018". I want it to say the whole day and whole month. I could not find a "get fullDate" method unfortunately..

window.onload = function() {
  var date = new Date();

  document.getElementById("date").innerHTML = date.toDateString();
}
<div id="date"> </div>

I am trying to get the full date with my function. Right now my results are "Wed Feb 14 2018", but I would like it to say "Wednesday February 2018". I want it to say the whole day and whole month. I could not find a "get fullDate" method unfortunately..

window.onload = function() {
  var date = new Date();

  document.getElementById("date").innerHTML = date.toDateString();
}
<div id="date"> </div>

Share Improve this question edited Feb 14, 2018 at 20:13 Jason Cust 10.9k2 gold badges36 silver badges45 bronze badges asked Feb 14, 2018 at 20:11 AlisonAlison 2591 gold badge3 silver badges14 bronze badges 2
  • @GordonKushner thanks! used it before, but does not work. When using that, I get the timestamp aswell with the Wed Feb 14 2018 – Alison Commented Feb 14, 2018 at 20:18
  • Possible duplicate of Get month name from Date – Sajid Hasan Apon Commented Feb 14, 2018 at 20:19
Add a ment  | 

3 Answers 3

Reset to default 9

You're actually looking for the toLocaleString function, as it can be customized pretty heavily without losing your date object.

window.onload = function() {
  var date = new Date();

  document.getElementById("date").innerHTML = date.toLocaleString('en-US', {weekday: 'long', month: 'long', year: 'numeric'});
}
<div id="date"> </div>

If you are looking for native date methods:

function dateConvert(){
      let date = new Date();
      let year = date.getFullYear();
      let month= date.getMonth();
      let day = date.getDay();
  
      let months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
      let days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
  
      return converted_date = `${days[day]} ${months[month]}  ${year}`;
    }
    
document.write(dateConvert());

As you have noticed, the native Date methods are a bit limited. There are a handful of roll-your-own approaches and if this is all you need then that is probably what you should use. If you need something more robust I would remend a library to avoid running over many pitfalls in attempting to handle the craziness of formatting time correctly.

A very popular library is called momentjs. A very simple solution for your issue is to use their format method:

const date = new Date();

document.getElementById("date").innerHTML = moment().format("dddd MMMM YYYY");
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<div id="date"> </div>

Hope this helps!

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

相关推荐

  • html - How to get the full date with javascript? - Stack Overflow

    I am trying to get the full date with my function. Right now my results are "Wed Feb 14 2018"

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信