admin管理员组文章数量:1430595
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
3 Answers
Reset to default 9You'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!
本文标签: htmlHow to get the full date with javascriptStack Overflow
版权声明:本文标题:html - How to get the full date with javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483215a2660268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论