可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
i am trying to convert a string in the format dd-mm-yyyy into a date object in JavaScript using the following:
var from = $("#datepicker").val(); var to = $("#datepickertwo").val(); var f = new Date(from); var t = new Date(to);
("#datepicker").val() contains a date in the format dd-mm-yyyy. When I do the following, I get "Invalid Date":
alert(f);
Is this because of the '-' symbol? How can I overcome this?
回答1:
Parse the string into the parts you need:
var from = $("#datepicker").val().split("-"); var f = new Date(from[2], from[1] - 1, from[0]);
Why not use a regex?
Because you know you'll be working on a string made up of three parts, separated by hyphens.
However, if you were looking for that same string within another string, regex would be the way to go.
Reuse
Because you're doing this more than once in your sample code, and maybe elsewhere in your code base, wrap it up in a function:
function toDate(dateStr) { var parts = dateStr.split("-"); return new Date(parts[2], parts[1] - 1, parts[0]); }
Using as:
var from = $("#datepicker").val(); var to = $("#datepickertwo").val(); var f = toDate(from); var t = toDate(to);
Or if you don't mind jQuery in your function:
function toDate(selector) { var from = $(selector).val().split("-"); return new Date(from[2], from[1] - 1, from[0]); }
Using as:
var f = toDate("#datepicker"); var t = toDate("#datepickertwo");
Modern JavaScript
If you're able to use more modern JS, array destructuring is a nice touch also:
function toDate(dateStr) { const [day, month, year] = dateStr.split("-") return new Date(year, month - 1, day) }
回答2:
regular expression example:
new Date( "13-01-2011".replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3") );
回答3:
Another possibility:
var from = "10-11-2011"; var numbers = from.match(/\d+/g); var date = new Date(numbers[2], numbers[0]-1, numbers[1]);
Match the digits and reorder them
回答4:
var from = $("#datepicker").val(); var f = $.datepicker.parseDate("d-m-Y", from);
回答5:
Use this format: myDate = new Date('2011-01-03'); // Mon Jan 03 2011 00:00:00
回答6:
You can also write a date inside the parentheses of the Date() object, like these:
new Date("Month dd, yyyy hh:mm:ss") new Date("Month dd, yyyy") new Date(yyyy,mm,dd,hh,mm,ss) new Date(yyyy,mm,dd) new Date(milliseconds)
回答7:
You can use an external library to help you out.
http://www.mattkruse.com/javascript/date/source.html
getDateFromFormat(val,format);
Also see this: Parse DateTime string in JavaScript
回答8:
In my case
new Date("20151102034013".replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/, "$1-$2-$3T$4:$5:$6"))
Result: Mon Nov 02 2015 04:40:13 GMT+0100 (CET) then I use .getTime() to work with milliseconds
回答9:
Using moment.js example:
var from = '11-04-2017' // OR $("#datepicker").val(); var milliseconds = moment(from, "DD-MM-YYYY").format('x'); var f = new Date(milliseconds)
回答10:
Take a look at Datejs for all those petty date related issues.. You could solve this by parseDate function too
回答11:
new Date().toLocaleDateString();
simple as that, just pass your date to js Date Object
回答12:
You could use a Regexp.
var result = /^(\d{2})-(\d{2})-(\d{4})$/.exec($("#datepicker").val()); if (result) { from = new Date( parseInt(result[3], 10), parseInt(result[2], 10) - 1, parseInt(result[1], 10) ); }