久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  1. <legend id='r4O10'><style id='r4O10'><dir id='r4O10'><q id='r4O10'></q></dir></style></legend>

      • <bdo id='r4O10'></bdo><ul id='r4O10'></ul>

      <small id='r4O10'></small><noframes id='r4O10'>

      <tfoot id='r4O10'></tfoot>
    1. <i id='r4O10'><tr id='r4O10'><dt id='r4O10'><q id='r4O10'><span id='r4O10'><b id='r4O10'><form id='r4O10'><ins id='r4O10'></ins><ul id='r4O10'></ul><sub id='r4O10'></sub></form><legend id='r4O10'></legend><bdo id='r4O10'><pre id='r4O10'><center id='r4O10'></center></pre></bdo></b><th id='r4O10'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='r4O10'><tfoot id='r4O10'></tfoot><dl id='r4O10'><fieldset id='r4O10'></fieldset></dl></div>
    2. 你如何保存從瀏覽器到服務(wù)器的 JavaScript 日期的

      How do you preserve a JavaScript date#39;s time zone from browser to server, and back?(你如何保存從瀏覽器到服務(wù)器的 JavaScript 日期的時區(qū),然后返回?)

            <tbody id='ebLmk'></tbody>

          <legend id='ebLmk'><style id='ebLmk'><dir id='ebLmk'><q id='ebLmk'></q></dir></style></legend>
            <bdo id='ebLmk'></bdo><ul id='ebLmk'></ul>
          • <i id='ebLmk'><tr id='ebLmk'><dt id='ebLmk'><q id='ebLmk'><span id='ebLmk'><b id='ebLmk'><form id='ebLmk'><ins id='ebLmk'></ins><ul id='ebLmk'></ul><sub id='ebLmk'></sub></form><legend id='ebLmk'></legend><bdo id='ebLmk'><pre id='ebLmk'><center id='ebLmk'></center></pre></bdo></b><th id='ebLmk'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ebLmk'><tfoot id='ebLmk'></tfoot><dl id='ebLmk'><fieldset id='ebLmk'></fieldset></dl></div>

              <small id='ebLmk'></small><noframes id='ebLmk'>

              • <tfoot id='ebLmk'></tfoot>
                本文介紹了你如何保存從瀏覽器到服務(wù)器的 JavaScript 日期的時區(qū),然后返回?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                例如,使用日期和時間控件,用戶選擇日期和時間,使得字符串表示如下:

                For example, using a date and time control, the user selects a date and time, such that the string representation is the following:

                "6-25-2012 12:00:00 PM"
                

                這個用戶恰好在 EST 時區(qū).該字符串被傳遞到服務(wù)器,服務(wù)器將其轉(zhuǎn)換為 .NET DateTime 對象,然后將其存儲在 SQL Server 中的 datetime 列中.

                It so happens that this user is in the EST time zone. The string is passed to the server, which translates it into a .NET DateTime object, and then stores it in SQL Server in a datetime column.

                當(dāng)稍后將日期返回給瀏覽器時,需要將其轉(zhuǎn)換回日期,但是當(dāng)將上述字符串輸入日期時,它會損失 4 小時的時間.我相信這是因為在創(chuàng)建 JavaScript 日期時未指定時區(qū)時,它默認(rèn)為本地時間,并且由于 EST 是格林威治標(biāo)準(zhǔn)時間的 -400,它會從下午 12 點減去 4 小時,即使下午 12 點本應(yīng)指定為 EST 時用戶在 EST 時區(qū)的機器上選擇了它.

                When the date is returned later to the browser, it needs to be converted back into a date, however when the above string is fed into a date it is losing 4 hours of time. I believe this is because when not specifying a timezone while creating a JavaScript date, it defaults to local time, and since EST is -400 from GMT, it subtracts 4 hours from 12pm, even though that 12pm was meant to be specified as EST when the user selected it on a machine in the EST time zone.

                顯然需要在原始日期時間字符串中添加一些內(nèi)容,然后才能將其傳遞給服務(wù)器以進(jìn)行持久化.這樣做的推薦方法是什么?

                Clearly something needs to be added to the original datetime string before its passed to the server to be persisted. What is the recommended way of doing this?

                推薦答案

                不要依賴 JavaScript 的 Date 構(gòu)造函數(shù)來解析字符串.行為和支持的格式因瀏覽器和語言環(huán)境而異.如果您使用 Date一些 的默認(rèn)行為> 直接對象.

                Don't rely on JavaScript's Date constructor to parse a string. The behavior and supported formats vary wildly per browser and locale. Here are just some of the default behaviors if you use the Date object directly.

                如果您必須來自字符串,請嘗試使用標(biāo)準(zhǔn)化格式,例如 ISO8601.您以該格式提供的日期將是 "2012-06-25T12:00:00".在 JavaScript 中使用這些最簡單的方法是使用 moment.js.

                If you must come from a string, try using a standardized format such as ISO8601. The date you gave in that format would be "2012-06-25T12:00:00". The easiest way to work with these in JavaScript is with moment.js.

                另外,請注意您實際要表達(dá)的意思.現(xiàn)在,您正在傳遞本地日期/時間,保存本地/日期/時間,并返回本地日期/時間.在此過程中,本地"的概念可能會發(fā)生變化.

                Also, be careful about what you are actually meaning to represent. Right now, you are passing a local date/time, saving a local/date/time, and returning a local date/time. Along the way, the idea of what is "local" could change.

                在許多情況下,日期/時間旨在表示準(zhǔn)確時刻.為此,您需要在客戶端將輸入的本地時間轉(zhuǎn)換為 UTC.將 UTC 發(fā)送到您的服務(wù)器并存儲它.稍后,檢索 UTC 并將其發(fā)送回您的客戶端,將其作為 UTC 處理并轉(zhuǎn)換回本地時間.您可以使用 moment.js 輕松完成所有這些工作:

                In many cases, the date/time is intended to represent an exact moment in time. To make that work, you need to convert from the local time entered to UTC on the client. Send UTC to your server, and store it. Later, retrieve UTC and send it back to your client, process it as UTC and convert back to local time. You can do all of this easily with moment.js:

                // I'll assume these are the inputs you have.  Adjust accordingly.
                var dateString = "6-25-2012";
                var timeString = "12:00:00 PM";
                
                // Construct a moment in the default local time zone, using a specific format.
                var m = moment(dateString + " " + timeString, "M-D-YYYY h:mm:ss A");
                
                // Get the value in UTC as an ISO8601 formatted string
                var utc = m.toISOString(); // output: "2012-06-25T19:00:00.000Z"
                

                在.Net的服務(wù)器上:

                On the server in .Net:

                var dt = DateTime.Parse("2012-06-25T19:00:00.000Z",   // from the input variable
                                        CultureInfo.InvariantCulture, // recommended for ISO
                                        DateTimeStyles.RoundtripKind) // honor the Z for UTC kind
                

                將其存儲在數(shù)據(jù)庫中.稍后取回并發(fā)送回去:

                Store that in the database. Later retrieve it and send it back:

                // when you pull it from your database, set it to UTC kind
                var dt = DateTime.SpecifyKind((DateTime)reader["yourfield"], DateTimeKind.Utc);
                
                // send it back in ISO format:
                var s = dt.ToString("o"); // "o" is the ISO8601 "round-trip" pattern.
                

                將它傳回moment.js中的javascript:

                Pass it back to the javascript in moment.js:

                // construct a moment:
                var m = moment("2012-06-25T19:00:00.000Z"); // use the value from the server
                
                // display it in this user's local time zone, in whatever format you want
                var s = m.format("LLL");   // "June 25 2012 12:00 PM"
                
                // or if you need a Date object
                var dt = m.toDate();
                

                看 - 這很容易,而且您無需對時區(qū)進(jìn)行任何花哨的操作.

                See - that was easy, and you didn't need to get into anything fancy with time zones.

                這篇關(guān)于你如何保存從瀏覽器到服務(wù)器的 JavaScript 日期的時區(qū),然后返回?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調(diào)用完成)
                JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標(biāo)頭) - IT屋-程序員軟件開發(fā)技術(shù)分
                Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)
                Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)
                • <legend id='j0mZi'><style id='j0mZi'><dir id='j0mZi'><q id='j0mZi'></q></dir></style></legend>

                    <i id='j0mZi'><tr id='j0mZi'><dt id='j0mZi'><q id='j0mZi'><span id='j0mZi'><b id='j0mZi'><form id='j0mZi'><ins id='j0mZi'></ins><ul id='j0mZi'></ul><sub id='j0mZi'></sub></form><legend id='j0mZi'></legend><bdo id='j0mZi'><pre id='j0mZi'><center id='j0mZi'></center></pre></bdo></b><th id='j0mZi'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='j0mZi'><tfoot id='j0mZi'></tfoot><dl id='j0mZi'><fieldset id='j0mZi'></fieldset></dl></div>

                    <small id='j0mZi'></small><noframes id='j0mZi'>

                  1. <tfoot id='j0mZi'></tfoot>

                        <tbody id='j0mZi'></tbody>
                      • <bdo id='j0mZi'></bdo><ul id='j0mZi'></ul>
                        1. 主站蜘蛛池模板: 精品成人av| 久久久精品网 | 亚洲男人网 | 国产精品毛片久久久久久久 | 欧美日韩久久久久 | 久久伊人一区二区 | 欧美日韩一二三区 | 中文字幕亚洲精品 | 欧美专区日韩专区 | 久久成人综合 | 91影院 | 成人av电影网 | 国产精品永久免费 | 久久久xx | 国产亚洲一区二区三区 | 一区二区免费视频 | 中文字幕av在线一二三区 | 中文字幕在线看 | 亚州激情| 在线观看国产精品视频 | 99亚洲国产精品 | 日本不卡视频在线播放 | 日本午夜视频 | 午夜小视频在线播放 | 亚洲精品一区在线 | 日本在线综合 | 午夜亚洲| 久久精品亚洲精品国产欧美 | 精品久久久久久亚洲综合网站 | 亚洲成人三区 | 91精品久久久久久久99 | 精品成人一区二区 | 国产色网| 国产一区二区三区在线 | 欧美一区二区三区在线播放 | 一级毛片视频 | 黄色三级在线播放 | 亚洲女人的天堂 | 色婷婷在线视频 | 成人视屏在线观看 | 狠狠躁天天躁夜夜躁婷婷老牛影视 |