比较不同格式的日期 [英] Compare dates of different format

查看:318
本文介绍了比较不同格式的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用日期选择器来选择日期(仅限月份和年份)。我也在使用Angular JS。当数据以 2014-08-12T13:09:00 格式绑定时,当我从日期选择器中选择任何数据时,格式为 8月1日星期五18:52:21 UTC + 0530 2014



如何仅将这两个日期与月份和年份进行比较。



是否可以将JSON字符串转换为仅包含月份和年份的格式,然后比较???





请帮帮我

I am using a datepicker to select dates (month and year only). I am using Angular JS too. When the data is bound its in format 2014-08-12T13:09:00 and when I select any from date picker its in format Fri Aug 1 18:52:21 UTC+0530 2014

How can I compare these two dates with month and year only.

Is it possible to convert to JSON string into format that has month and year only and then compare???


Please help me

推荐答案

比较客户端的一种方法是从每个有效字符串,获取月份/年份并进行比较,类似于:

One way to compare client-side would be to make a date object from each valid string, get the month/year and compare, similar to this:
var date1 = new Date('Fri Aug 1 18:52:21 UTC+0530 2014');
var date2 = new Date('2014-08-12T13:09:00');

// gets month and year values - month is zero based so +1 ensures correct month, though if you simply want to compare values you don't need to do +1 nor the `/`! - see details below
var date1Short = date1.getMonth() + 1 + '/' + date1.getFullYear(); // date1Short is now '8/2014'
var date2Short = date2.getMonth() + 1 + '/' + date2.getFullYear(); // date2Short is now '8/2014'

// Now compare your values
if(date1Short === date2Short){
  //....
} else {
   //....
}



DEMO - 比较月份和年份值




替代格式


DEMO - Comparing month and year values



Alternative Formatting

// if values are only used for compare then no +1 or '/' is needed and the below will work too
// + '' is needed below to prevent addition (7+2014) and ensure string concatenation instead (72014)

var date1Short = date1.getMonth() + '' + date1.getFullYear(); // date1Short would then be '72014';
var date2Short = date2.getMonth() + '' + date2.getFullYear(); // date2Short would then be '72014';



如果需要比较服务器端,请使用相同的getMonth()/ getFullYear()...方法将值传递给服务器。


If you need to compare server side, use the same getMonth()/getFullYear()... methods to pass the values to the server.






感谢您的回复。我创建了一个JSOn日期并对其进行了比较
Hi,

Thanks for the response. I created a JSOn date and compared it


这篇关于比较不同格式的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆