开发者

jQuery - Templates - How to do a timestamp compare

开发者 https://www.devze.com 2023-02-14 23:09 出处:网络
I\'m using jQuery Templates, and want to do a timestamp compare. I开发者_如何学Gos this possible?

I'm using jQuery Templates, and want to do a timestamp compare. I开发者_如何学Gos this possible?

I was hoping to do something along the lines of:

<script id='mytemplate' type='text/html'>
{{if 2011-03-02T14:15:04-08:00 > Current_timestamp}}
New
{{else}}
not new
{{/if}}
</script>

possible? Thanks

Update, realized client-side time will not work and need to pass the current server time: Here is the latest :

<script id="comment" type="text/html">

    {{if new Date(cmtModel.conv_last_reply()) > new Date(created_at) }}
        yep
    {{else}}
        nope
    {{/if}}

</script>

Where cmtModel.conv_last_reply() is coming from KnockoutJS, and created_at is when the comment was created.


You can compare JavaScript Date objects.

UPDATE looks like template gets caught on directly comparing dates inside the if. You can do something like this:

<script id="template" type="text/x-jquery-tmpl">
    {{if item}}
    New
    {{else}}
    not new
    {{/if}}
</script>

Then your javascript could look like this:

var dateDiff = new Date('2011-03-02T14:15:04-08:00') > new Date();
$('#template').tmpl(dateDiff);

See example: http://jsfiddle.net/yads/ScwTP/2/

0

精彩评论

暂无评论...
验证码 换一张
取 消