英文:
input type date format should be 12-mar-23 even if user type 12-03-23
问题
(function() {
$('#submit').on('click', function(){
var date = new Date($('#date-input').val());
day = date.getDate();
month = date.toLocaleString('en-us', { month: 'short' });
year = date.getFullYear().toString().slice(-2);
alert([day, month, year].join('/'));
});
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="date-input" required />
<button id="submit">Submit</button>
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
(function() {
$('#submit').on('click', function(){
var date = new Date($('#date-input').val());
day = date.getDate();
month = date.getMonth() + 1;
year = date.getFullYear();
alert([day, month, year].join('/'));
});
})();
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="date-input" required />
<button id="submit">Submit</button>
<!-- end snippet -->
In input date, format should be 01-mar-23
even if the user type in this format 01-03-23
The required format be 01-mar-23
答案1
得分: 1
你也可以按照你的日期格式获取月份的名称,像这样,
const monthNames = [
"jan", "feb", "mar", "apr",
"May", "jun", "jul", "aug",
"sept", "oct", "nov", "dec"
];
(function() {
$('#submit').on('click', function() {
var d = new Date($('#date-input').val());
alert([String(d.getDate()).padStart(2, '0'), monthNames[d.getMonth()], d.getFullYear().toString().substr(-2)].join('-'));
});
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="date-input" required />
<button id="submit">Submit</button>
英文:
You can also get the month name in your date format like this,
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const monthNames = [
"jan", "feb", "mar", "apr",
"May", "jun", "jul", "aug",
"sept", "oct", "nov", "dec"
];
(function() {
$('#submit').on('click', function() {
var d = new Date($('#date-input').val());
alert([String(d.getDate()).padStart(2, '0'), monthNames[d.getMonth()], d.getFullYear().toString().substr(-2)].join('-'));
});
})();
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="date-input" required />
<button id="submit">Submit</button>
<!-- end snippet -->
答案2
得分: 0
You can use this format for a date to convert the month to a string:
var date = new Date($('#date-input').val());
const day = date.getDate().toString().padStart(2, '0');
const month = date.toLocaleString('default', { month: 'short' }).toLowerCase();
const year = date.getFullYear().toString().substr(-2);
const formattedDate = `${day}-${month}-${year}`;
console.log(formattedDate); // Output: "01-mar-23"
(Note: I've omitted the translation of the code part as per your request.)
英文:
You can use this Format for a date to convert month to string
var date = new Date($('#date-input').val());
const day = date.getDate().toString().padStart(2, '0');
const month = date.toLocaleString('default', { month: 'short' }).toLowerCase();
const year = date.getFullYear().toString().substr(-2);
const formattedDate = `${day}-${month}-${year}`;
console.log(formattedDate); // Output: "01-mar-23"
答案3
得分: 0
你要求的以字符串格式显示月份的日期不受浏览器原生日期选择器支持。
尽管如此,你可以通过在3个“选择框”中分别选择日、月和年来实现所需的格式,用户可以从月份下拉菜单中进行选择,我们可以根据需要传递选项,例如,
- 第一个选项(一月,Jan,jan,1 等等...)
英文:
The date with month in string format you are asking for is not supported in browser native datepickers.
Although, you can achieve the format you need by seperating the day, month and year in 3 select boxes
, where user can select from the month dropdown, and in that we can pass options according to our requirement like,
- 1st option (January, Jan, jan, 1, etc...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论