更改日期选择器中的日期格式

发布于 2025-01-13 03:43:58 字数 958 浏览 4 评论 0原文

我有一个关于将日期格式更改为 dd/mm/yyyy 的问题。我希望默认日期为 09-01-2022。但下面的代码无法更改 dd/mm/yyyy。

var today = "Wed Jan 09 2022 13:01:18 GMT+0800 (Malaysia Time)";
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
<div class="col-xs-12 col-md-2">
<label>Date&nbsp;:</label>
<input type="date" class="form-control" id="date_of_daily" name="date_of_daily" value="2022-01-09" title="Date of daily" min="2022-01-09" max="2022-01-11">
</div>

实际上我想要如下图所示的结果:

output

希望有人能指导我如何解决这个问题。谢谢。

I have a question about changing the date format to dd/mm/yyyy. I want the default date to be 09-01-2022. But below the code doesn't work to change dd/mm/yyyy.

var today = "Wed Jan 09 2022 13:01:18 GMT+0800 (Malaysia Time)";
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
<div class="col-xs-12 col-md-2">
<label>Date :</label>
<input type="date" class="form-control" id="date_of_daily" name="date_of_daily" value="2022-01-09" title="Date of daily" min="2022-01-09" max="2022-01-11">
</div>

Actually I want the result like below picture:

output

Hope someone can guide me on how to solve this problem. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情栀口红 2025-01-20 03:43:58

你可以尝试一下:

var today = "Wed Jan 09 2022 13:01:18 GMT+0800 (Malaysia Time)";

function convertDate(inputFormat) {
  function pad(s) { return (s < 10) ? '0' + s : s; }
  var d = new Date(inputFormat)
  return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/')
}

console.log(convertDate(today))

$(function() {
  $("#date_of_daily").datepicker({
    dateFormat: "dd/mm/yy",
    minDate: 0,
    maxDate: "+2D +0M",
    showAnim: "slideDown"
  });
});
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://jqueryui.com/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
<div class="col-xs-12 col-md-2">
  <label>Date :</label>
  <input type="text" class="form-control" id="date_of_daily" name="date_of_daily" size="30" value="09/01/2022" title="Date of daily" min="2022-01-09" max="2022-01-11">
</div>

You can try it with :

var today = "Wed Jan 09 2022 13:01:18 GMT+0800 (Malaysia Time)";

function convertDate(inputFormat) {
  function pad(s) { return (s < 10) ? '0' + s : s; }
  var d = new Date(inputFormat)
  return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/')
}

console.log(convertDate(today))

$(function() {
  $("#date_of_daily").datepicker({
    dateFormat: "dd/mm/yy",
    minDate: 0,
    maxDate: "+2D +0M",
    showAnim: "slideDown"
  });
});
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://jqueryui.com/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
<div class="col-xs-12 col-md-2">
  <label>Date :</label>
  <input type="text" class="form-control" id="date_of_daily" name="date_of_daily" size="30" value="09/01/2022" title="Date of daily" min="2022-01-09" max="2022-01-11">
</div>

花海 2025-01-20 03:43:58

我希望下面的内容能回答您的问题...您今天需要基本上解析字符串以将其转换为日期,然后再调用 Date 函数(例如 getDay 等)...

var today = "Wed Jan 09 2022 13:01:18 GMT+0800 (Malaysia Time)";
var dd = String(Date.parse(today)).padStart(2, '0');
var mm = String(new Date(Date.parse(today)).getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = new Date(Date.parse(today)).getFullYear();
today = mm + '/' + dd + '/' + yyyy;
<div class="col-xs-12 col-md-2">
<label>Date :</label>
<input type="date" class="form-control" id="date_of_daily" name="date_of_daily" value="2022-01-09" title="Date of daily" min="2022-01-09" max="2022-01-11">
</div>

I hope the below answers your question...You need to basically parse the string today to convert it to a date before calling functions of Date on it like getDay etc...

var today = "Wed Jan 09 2022 13:01:18 GMT+0800 (Malaysia Time)";
var dd = String(Date.parse(today)).padStart(2, '0');
var mm = String(new Date(Date.parse(today)).getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = new Date(Date.parse(today)).getFullYear();
today = mm + '/' + dd + '/' + yyyy;
<div class="col-xs-12 col-md-2">
<label>Date :</label>
<input type="date" class="form-control" id="date_of_daily" name="date_of_daily" value="2022-01-09" title="Date of daily" min="2022-01-09" max="2022-01-11">
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文