【问题标题】:.toDateString without the 'Day name' [duplicate].toDateString 没有“日期名称”[重复]
【发布时间】:2019-02-19 06:54:56
【问题描述】:

我正在使用此代码将日期转换为 ID 为“今天”的跨度。但是,现在的要求意味着我需要将一周中的实际日期排除在外。所以只是 2018 年 9 月 14 日(例如?)有没有办法在不拼出整个日期的情况下实现这一目标。

$('#today').html(d.toDateString());

【问题讨论】:

  • 使用库之类的时刻。
  • d.toDateString().substring(4)
  • Salman A 给出了完美的答案。你能解释一下它做了什么吗?它占用了该行的第 4 个字符串?
  • @conye9980 只需查看有关子字符串的文档
  • @conye9980 它返回从第 5 个字符开始的字符串(从零开始的索引 - 删除字符 0-4)。

标签: javascript date


【解决方案1】:

toDateString() 方法以人类可读的美式英语形式返回 Date 对象的日期部分。

你可以用空字符串分割字符串,然后按照你想要的方式重新排列字符串:

var d =  new Date().toDateString(); //Fri Sep 14 2018
d = d.split(' ');                   //["Fri","Sep","14","2018"]
$('#today').html(d[2] + ' ' + d[1] + ' ' + d[3]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="today"></span>

【讨论】:

    猜你喜欢
    • 2018-09-29
    • 2013-07-31
    • 2019-11-09
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多