在jquery中转换日期

时间:2013-06-11 18:20:14

标签: jquery asp.net

我在h3标签中有一个日期,我试图使用我的代码隐藏文件中的某些功能来抓取并转换为另一个日期。我如何在jquery中完成这个?

HTML

<div id="firstweekwrapper" class="weekcontainer">
                        <div id="day1" class="daytile daytilefirst">
                            <h3 id="day1date" class="dateh3"><%=FirstTradingDayInMonth(DateTime.Now.Date).ToLongDateString()%></h3><br />
                            <p id="day1equities"><span class="targettext"> <span class="bluetext">Equities</span> Target PnL:&nbsp;</span><span id="day1equitytargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Equities</span> PnL:&nbsp;</span><span id="day1equityactualvalue" class="actualtext"></span></p>
                            <p id="day1options"><span class="targettext"> <span class="bluetext">Options</span> Target PnL:&nbsp;</span><span id="day1optiontargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Options</span> PnL:&nbsp;</span><span id="day1optionactualvalue" class="actualtext"></span></p>
                            <p id="day1futures"><span class="targettext"> <span class="bluetext">Futures</span> Target PnL:&nbsp;</span><span id="day1futuretargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Futures</span> PnL:&nbsp;</span><span id="day1futureactualvalue" class="actualtext"></span></p>
                        </div>
                        <div id="day2" class="daytile">
                            <h3 id="day2date" class="dateh3"></h3><br />
                            <p id="day2equities"><span class="targettext"> <span class="bluetext">Equities</span> Target PnL:&nbsp;</span><span id="day2equitytargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Equities</span> PnL:&nbsp;</span><span id="day2equityactualvalue" class="actualtext"></span></p>
                            <p id="day2options"><span class="targettext"> <span class="bluetext">Options</span> Target PnL:&nbsp;</span><span id="day2optiontargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Options</span> PnL:&nbsp;</span><span id="day2optionactualvalue" class="actualtext"></span></p>
                            <p id="day2futures"><span class="targettext"> <span class="bluetext">Futures</span> Target PnL:&nbsp;</span><span id="day2futuretargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Futures</span> PnL:&nbsp;</span><span id="day2futureactualvalue" class="actualtext"></span></p>
                        </div>
                         <div id="day3" class="daytile">
                            <h3 id="day3date" class="dateh3"></h3><br />
                            <p id="day3equities"><span class="targettext"> <span class="bluetext">Equities</span> Target PnL:&nbsp;</span><span id="day3equitytargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Equities</span> PnL:&nbsp;</span><span id="day3equityactualvalue" class="actualtext"></span></p>
                            <p id="day3options"><span class="targettext"> <span class="bluetext">Options</span> Target PnL:&nbsp;</span><span id="day3optiontargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Options</span> PnL:&nbsp;</span><span id="day3optionactualvalue" class="actualtext"></span></p>
                            <p id="day3futures"><span class="targettext"> <span class="bluetext">Futures</span> Target PnL:&nbsp;</span><span id="day3futuretargetvalue" class="targettext"></span><br /><span class="actualtext">Actual <span class="bluetext">Futures</span> PnL:&nbsp;</span><span id="day3futureactualvalue" class="actualtext"></span></p>
                        </div>

我正在尝试使用我的代码隐藏文件中的AddBusinessDay函数将day2date id的值设置为下一个工作日。我对使用jquery有些新意,所以我不确定如何使用代码隐藏文件中的vb.net代码进行合并。

1 个答案:

答案 0 :(得分:0)

我真的认为你应该做这个服务器端,但如果你打算使用javascript来做这件事,你可以这样做:

  • 将日期字符串解析为日期对象
  • 添加一天
  • 将日期转换回格式化日期字符串

DEMO

$("button").click(function () {
    var _date = Date.parse($("#day1date").text());
    $("#day2date").text(FormattedDate(_date, 1));
    $("#day3date").text(FormattedDate(_date, 2));
    $("#day4date").text(FormattedDate(_date, 3));
    $("#day5date").text(FormattedDate(_date, 4));
    $("#day6date").text(FormattedDate(_date, 5));
});

var d_names = new Array("Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday", "Friday", "Saturday");

var m_names = new Array("January", "February", "March",
    "April", "May", "June", "July", "August", "September",
    "October", "November", "December");

function FormattedDate(_date, _addDays) {
    var d = _date + (86400000 * _addDays); //86400000ms = 1day
    d = new Date(d);
    var returnDate = d_names[d.getDay()] + ", " + m_names[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
    return returnDate;
}