gpt4 book ai didi

javascript - 如何使用 javascript 以 YY-MM-DD 格式返回月份和年份?

转载 作者:行者123 更新时间:2023-11-28 00:40:30 24 4
gpt4 key购买 nike

我的文本框中有一个“2015-02-17”(yy-mm-dd) 格式的日期。现在我只想返回月份号 2 和年份 2015。我无法使用 getMonth() 函数正确执行此操作。

我尝试了以下方法:

 // Get date value from the textbox
var t_date = $("#t_date").val(); // 2015-01-17
alert(t_date.getMonth());

它不显示警报框。看来我的代码无效。请帮忙。谢谢

最佳答案

试试这个。

var t_date = new Date($("#t_date").val()); 
alert(t_date.getMonth() + 1); // because months are zero based.

关于javascript - 如何使用 javascript 以 YY-MM-DD 格式返回月份和年份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27997373/

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