简体   繁体   English

通过传递日期(数字),月份(名称),年份(数字)获取完整的javascript日期

[英]Get full javascript date from passing a day (number), month (name), year (number)

I'm trying to pass some date information to a date object to format the date based on where the user is hovering in a datepicker. 我试图将一些日期信息传递给日期对象,以根据用户在日期选择器中悬停的位置来格式化日期。

Using $(this) I'm able to get the day of the given month as a number. 使用$(this),我可以将给定月份的日期作为数字。 Using traversal i'm able to get the month name (February) and the year as a digit (2014). 使用遍历,我可以获取月份名称(2月)和年份作为数字(2014年)。

Is it possible to take these values and apply them to a UTC date string so I can compare with my datepicker? 是否可以采用这些值并将它们应用于UTC日期字符串,以便我可以与日期选择器进行比较?

So far i've got: 到目前为止,我已经:

var day = $(this).text();
var month = closest_datepicker.find('span.ui-datepicker-month').text();
var year = closest_datepicker.find('span.ui-datepicker-year').text();

which outputs '17 February 2014' 输出“ 2014年2月17日”

and ... well i'm not sure exactly the next step from here but I want to format this so that I either get the same format as my other dates: 而且...我不确定从这里开始的下一步是否正确,但是我想对此进行格式化,以便获得与其他日期相同的格式:

Wed Feb 12 2014 00:00:00 GMT+1100 (EST)

or a millisecond value like this: 1391748657216 或这样的毫秒值: 1391748657216

You can use a library like moment.js or: 您可以使用诸如moment.js之类的库,也可以使用:

var months = ['January', 'February', 'March']
var day = '17'
var month = 'February';
var year = 2014;

var date = new Date(+year, $.inArray(month, months), +day); // $.inArray() is used instead of Array.indexOf() because of IE compatibility
console.log(date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM