[英]Compare date to current month year
In one of the API I am using I have a date in the format yyyy-MM-dd hh:mm:ss
I am trying compare this date to just the current month and year. 在使用的一种API中,我有一个格式为
yyyy-MM-dd hh:mm:ss
日期,我正在尝试将此日期与当前月份和年份进行比较。 How do I just take out the month and year from that date and compare it in AngularJS? 我如何从该日期中取出月份和年份并在AngularJS中进行比较? I read the AngularJS Documentation for date formats and I can't seem just get the month and year from that format so that I can compare it with the current month and year
我阅读了AngularJS文档中的日期格式,我似乎无法仅从该格式中获取月份和年份,以便可以将其与当前月份和年份进行比较。
Refer momentjs docs. 请参阅momentjs文档。
Refer below code snippet, 请参阅以下代码段,
var apidate = moment('2019-01-01 11:31:23', 'YYYY-MM-DD hh:mm:ss'); // API date console.log("Day="+apidate.date()+ " Year="+apidate.year()); var now = moment(); // current date console.log("Day="+now.date()+ " Year="+now.year()); // compare day and year of both dates
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Using momentjs.com 使用momentjs.com
Convert the response from your API with eg; 转换来自API的响应,例如:
responseDate = moment(responseAPI).format('YYYYMM')
You can then you can compare it with an existing date eg; 然后,您可以将其与现有日期进行比较。
if (responseDate != moment(existingDate).format('YYYYMM')) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.