投稿時間:2023-04-23 10:10:20 RSSフィード2023-04-23 10:00 分まとめ(10件)
カテゴリー等 | サイト名等 | 記事タイトル・トレンドワード等 | リンクURL | 頻出ワード・要約等/検索ボリューム | 登録日 |
---|---|---|---|---|---|
IT | ITmedia 総合記事一覧 | [ITmedia ビジネスオンライン] ハーゲンダッツの新作は「映え」意識 スプーンで割るアイス、6年がかりで開発 | https://www.itmedia.co.jp/business/articles/2304/21/news186.html | crush | 2023-04-23 09:08:00 |
python | Pythonタグが付けられた新着投稿 - Qiita | Pythonでよく使うargsとkwargsについてまとめる | https://qiita.com/hirosuke1610/items/adbf8ddf12eef1aa0e85 | django | 2023-04-23 09:58:38 |
python | Pythonタグが付けられた新着投稿 - Qiita | ABC 299 備忘録 | https://qiita.com/mae-commits/items/82ceca61529031798871 | 自分 | 2023-04-23 09:39:10 |
Git | Gitタグが付けられた新着投稿 - Qiita | 【Git初心者向け】今更ながらGitをフレンチコースに見立てて解説してみた | https://qiita.com/ryotafull/items/b768e6ded03880ee7f27 | 解説 | 2023-04-23 09:30:45 |
技術ブログ | Developers.IO | 【レポート】これからの「動画」の話をしよう – 最新活用事例から見るメディア領域の進化と未来(AWS-48) #AWSSummit | https://dev.classmethod.jp/articles/aws-summit-tokyo-2023-aws-38/ | awsawssummit | 2023-04-23 00:04:12 |
海外TECH | DEV Community | Removing Timezones from Dates in Javascript | https://dev.to/shubhampatilsd/removing-timezones-from-dates-in-javascript-46ah | Removing Timezones from Dates in Javascript ForewordIt s been a while since I ve wrote on here I m planning to write smaller pieces on here that relate to problems that I solve on a day to day basis BackgroundI was working on the React Native app the other day for my travel planning startup Tripley and came across a somewhat unique problem When creating events for the trip calendar it would store dates with the current timezone in mind A Bit Too Much About TimezonesTo prevent one side of the world eating their lunches in darkness timezones were created PST Pacific Standard Time is hours behind EST Eastern Standard Time This means that in San Francisco is equivalent to in New York Each timezone gets a code relative to the timezone in London UK The timezone in London is GMT Greenwich Mean Time Why London Imperialism Universal Coordinated Time UTC is derived from GMT it s essentially the same time The main difference between UTC and London s time is that UTC doesn t care about daylight savings When The UK undergoes daylight savings and switches to the BST British Summer Time timezone UTC stays the same time as it was before Each timezone gets a code relative to the timezone in London UK Back to this Each timezone gets a time in hours relative to the UTC time PST s code is UTC which specifies that PST is hours behind UTC time This isn t limited to just hours Nepal for example has a code of UTC which specifies that it is hours and minutes ahead of UTC time BST even has a UTC code which is UTC In daylight savings time for Britain the BST timezone is one hour of UTC time The ProblemThis was a big problem for us Say that you selected August th at PM while sitting in San Francisco By the nature of JavaScript the date would be registered under UTC time in this case it would be August th at PM UTC For your trip if you travel to the island of Oahu Hawaii you would be under the HST timezone Hawaiian Standard Time which is hours behind PST Pacific Standard Time The event that you scheduled for PM would now show up at PM for you This would occur because JavaScript automatically adjusts UTC dates for you based on your current timezone It would convert the UTC date we had to the HST timezone Suppose we had a JS date variable called date and we ran this in San Francisco Wed Feb GMT Pacific Standard Time console log date Now if we ran this in Honolulu it would look a bit different Wed Feb GMT Hawaiian Aleutian Time console log date In either timezone if you print the date using the toISOString method you will get a date in the ISO format T Zconsole log date toISOString This ISO string is in UTC time If we dissect this string we can see that the T is a separator in the string So we have two portions and Z The first part is easily identifiable it s the date The second part is the time string Our original date in PST was but this date reads This is because the date is stored in UTC The PST timezone is hours behind UTC right If you add hours to you get The SolutionI tried a couple things when I learned about this First of all I tried to see if I could omit the timezone My first thought was to use the toLocaleString method Here is how it would look like const date new Date PM console log date toLocaleString I decided that I would put this string PM in my database I thought that I could then input this as a parameter to the Date constructor on the client like so new Date PM I tried the code above in the MDN Web Docs JS Date Playground with my computer set in PST and HST Code run in PST timezone Code run in HST timezone I looked at both results and they were the same I finally thought that this would be a great solution store the output of toLocaleString and convert it to a Date again on the client all without the timezone However when I implemented the solution in my React Native app it didn t work It kept telling me that I had passed an invalid date into the Date constructor I searched on the internet Then I found this on the MDN Web Docs This gave me a hint as to why the code worked in my browser but not my React Native app The JS in Firefox was running on a different runtime than React Native Short Note About JS EnginesSince JavaScript is so uncoordinated JS code typically runs in a specific environment called an engine Each browser has their own engine Chrome uses the V engine Firefox uses their SpiderMonkey engine and Safari uses JavaScriptCore In React Native s case it uses an engine called Hermes I was testing my code on Firefox which uses the SpiderMonkey engine and running my code in my React Native app which uses Hermes Within these two engines there must be a discrepancy between how the Date constructor parses a given string Second Round of SolutionsNow after hours that I realized that my first idea wouldn t work I came up with another If we create a Date object with the given ISO string the UTC time automatically gets converted to the current timezone const date new Date T Z Wed Feb GMT Pacific Standard Time console log date Now if we remove the Z from the end of the ISO string the timezone does not get automatically converted notice the changeconst date new Date T Wed Feb GMT Pacific Standard Time console log date The console log output changes from PM to PM Pseudocode So here was the idea that I had For simplicity purposes let s select a date amp time from the datepicker in our app Feb at PMIf we console log this Date object s ISO string we ll get T ZNotice how the date is one day ahead and the time is hours ahead of our given time We can offset this difference simply by subtracting hours since I created this in the PST timezone from the UTC time If we do that subtract hours we get T ZHowever if we create a new Date object and console log that we get this string Wed Feb GMT Pacific Standard Time We can still see that the UTC time is being converted to the local time We can fix that by chopping off that trailing Z character in the ISO string This is what the resulting ISO string would look like T Now if we console log the given Date object in San Francisco we get this string Wed Feb GMT Pacific Standard Time Now despite the timezone information still being there in the console log if we run the same code in Honolulu we ll getWed Feb GMT Hawaiian Aleutian Time The two dates line up Now here is the actual implementation for the pseudocode export const dateWithoutTimezone date Date gt const tzoffset date getTimezoneOffset offset in milliseconds const withoutTimezone new Date date valueOf tzoffset toISOString slice return withoutTimezone First tzoffset initializes a new Date and get the timezone offset in milliseconds in my case it would be milliseconds Then we subtract that from the value of date which was PM so it would now be PM Then we cut off the trailing Z from the ISO String so that JS doesn t consider it in UTC and convert it And there we go The function returns an ISO String that we can now store in our databases To parse this date again you can simply run new Date T and if we console log that we ll getWed Feb GMT Hawaiian Aleutian Time EndHey thanks for reading my blog I m planning on writing small blogs if schoolwork doesn t get in the way It s been a while since I last blogged but in that time I ve founded Tripley an app that helps you plan and go on trips much more easily It would help us out a ton if you could sign up for our waitlist and give us a follow on Twitter and Instagram Thanks If you want to see more of my daily thoughts follow my Twitter much appreciated | 2023-04-23 00:43:58 |
ニュース | BBC News - Home | Sudan fighting: Limited evacuation of foreigners begins | https://www.bbc.co.uk/news/world-africa-65363586?at_medium=RSS&at_campaign=KARANGA | foreign | 2023-04-23 00:44:35 |
ニュース | BBC News - Home | Tory peer Lord Maude calls for more robust culture in Whitehall | https://www.bbc.co.uk/news/uk-politics-65363845?at_medium=RSS&at_campaign=KARANGA | civil | 2023-04-23 00:23:13 |
ビジネス | 東洋経済オンライン | みずほの「Jコイン」、誕生4年で打ち出す挽回策 個人利用で苦戦の中、法人・自治体需要に活路 | 金融業界 | 東洋経済オンライン | https://toyokeizai.net/articles/-/667941?utm_source=rss&utm_medium=http&utm_campaign=link_back | 東洋経済オンライン | 2023-04-23 09:50:00 |
ビジネス | プレジデントオンライン | 稲盛さんが「心の世界」とか言うのはちょっと違うな…そう思っていたニデック永守重信会長が最近痛感したこと - 「財界人が坊主になる必要はない」と考えていたが… | https://president.jp/articles/-/68601 | 日本電産 | 2023-04-23 10:00:00 |
コメント
コメントを投稿