简体   繁体   English

将日期字符串转换为数字以进行比较

[英]Converting date string to number for comparison

I have dates in this format: 我有这种格式的日期:

Jan. 4, 2011 2011年1月4日

I need to convert these dates to numbers then compare to make sure sort is correct (ascending or descending). 我需要将这些日期转换为数字,然后进行比较以确保排序正确(升序或降序)。

I've tried using Date.parse(s), but I keep getting NaN back as value. 我尝试使用Date.parse(s),但我一直在将NaN作为值返回。 I believe it's the period after the 3 letter abbreviation thats screwing it up. 我相信这是3个字母缩写之后的一段时期。

How do I convert these date string to number? 如何将这些日期字符串转换为数字?

var myDate = "Jan. 4, 2011";
myDate = myDate.replace('.','');
var dateType = Date.parse(myDate);

dateType should give you a number dateType应该给你一个数字

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

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