gpt4 book ai didi

javascript - 从 Javascript Datepicker 获取工作日名称

转载 作者:行者123 更新时间:2023-12-02 17:45:36 25 4
gpt4 key购买 nike

我有一个代码片段,它为我提供了从 JSdatepicker 库的日历中选择的日期。

我正在寻找一种方法来获取所选日期的工作日名称,例如:星期一、星期二等...

下面是jsDatepicker的用法:

<script type="text/javascript">
window.onload = function(){
new JsDatePick({
useMode:2,
target:"inputField1",
dateFormat:"%Y-%m-%d",
cellColorScheme:"beige"
});

};
</script>

我查看了 jsDatePicker 中给出的文档但什么也没得到。有人可以帮忙吗?

我也尝试过使用 jQuery Datepicker,但它根本不起作用,下面是 jQuery datepicker 代码:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>

对于这两种情况,我要选择和显示日期的输入字段是:

<input type="text" id="datepicker"/>

最佳答案

如果您想使用JsDatePick ,这是一种方法:

// Save returning object to variable
var dateSelector = new JsDatePick({
useMode:2,
target:"inputField1",
dateFormat:"%Y-%m-%d",
cellColorScheme:"beige"
});

// Use setOnSelectedDelegate to capture clicks
dateSelector.setOnSelectedDelegate(function(){
// Get selected day from plug-in
var day = dateSelector.getSelectedDay();

// Create a JS native Date
var date = new Date(day.year, day.month, day.day);

// Get the day of the week (0 is Sunday)
var dayOfTheWeek = date.getDay();

// For demonstration purposes, place a string into input field
var inputField = document.getElementById("inputField1");
var dayOfTheWeekStr = '';

switch (dayOfTheWeek) {
case 0:
dayOfTheWeekStr = 'Sunday';
break;
case 1:
dayOfTheWeekStr = 'Monday';
break;
case 2:
dayOfTheWeekStr = 'Tuesday';
break;
default:
dayOfTheWeekStr = 'Wed.-Sat.';
break;
}

inputField.value = dayOfTheWeekStr;
});

这是一个JSFiddle .

关于javascript - 从 Javascript Datepicker 获取工作日名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21798740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com