stackoverflow.com
Open in
urlscan Pro
151.101.65.69
Public Scan
URL:
https://stackoverflow.com/questions/43496920/angular-http-post-changing-date-to-utc-date
Submission: On December 15 via manual from US
Submission: On December 15 via manual from US
Form analysis
3 forms found in the DOMGET /search
<form id="search" action="/search" method="get" class="searchbar" autocomplete="off" role="search">
<input name="q" type="text" placeholder="Search…" value="" tabindex="1" autocomplete="off" maxlength="240" class="f-input js-search-field">
<button type="submit" aria-label="Search..." class="btn-topbar-primary js-search-submit"><svg aria-hidden="true" class="svg-icon iconSearch" width="18" height="18" viewBox="0 0 18 18">
<path d="M12.86 11.32L18 16.5 16.5 18l-5.18-5.14v-.35a7 7 0 1 1 1.19-1.19h.35zM7 12A5 5 0 1 0 7 2a5 5 0 0 0 0 10z"></path>
</svg></button>
</form>
POST /users/signup?ssrc=hero&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f43496920%2fangular-http-post-changing-date-to-utc-date
<form id="login-form" action="/users/signup?ssrc=hero&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f43496920%2fangular-http-post-changing-date-to-utc-date" method="POST">
<input type="hidden" name="fkey" value="a98ac895ae44dffe99803060b762b991">
<input type="hidden" name="ssrc" value="">
<input type="hidden" id="oauth_version" name="oauth_version" value="">
<input type="hidden" id="oauth_server" name="oauth_server" value="">
<div id="se-login-fields">
<input type="hidden" name="legalLinksShown" value="0">
</div>
</form>
POST /questions/43496920/answer/submit
<form id="post-form" action="/questions/43496920/answer/submit" method="post" class="post-form">
<input type="hidden" id="post-id" value="43496920">
<input type="hidden" id="qualityBanWarningShown" name="qualityBanWarningShown" value="false">
<input type="hidden" name="referrer" value="">
<h2 class="space">Your Answer</h2>
<script>
StackExchange.ifUsing("editor", function() {
StackExchange.using("externalEditor", function() {
StackExchange.using("snippets", function() {
StackExchange.snippets.init();
});
});
}, "code-snippets");
</script>
<script>
StackExchange.ready(function() {
initTagRenderer("".split(" "), "".split(" "));
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
} else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer",
immediatelyShowMarkdownHelp: true
});
}
});
</script>
<div id="post-editor" class="post-editor js-post-editor">
<div style="position: relative;">
<div class="wmd-container">
<div id="wmd-button-bar" class="wmd-button-bar">
<ul id="wmd-button-row" class="wmd-button-row">
<li id="wmd-bold-button" class="wmd-button" style="left: 0px;"><span style="background-position: 0px -20px;"></span></li>
<li id="wmd-italic-button" class="wmd-button" style="left: 25px;"><span style="background-position: -20px -20px;"></span></li>
<li id="wmd-spacer1" class="wmd-spacer" style="left: 50px;"><span style="background-position: -40px -20px;"></span></li>
<li id="wmd-link-button" class="wmd-button" style="left: 75px;"><span style="background-position: -40px -20px;"></span></li>
<li id="wmd-quote-button" class="wmd-button" style="left: 100px;"><span style="background-position: -60px -20px;"></span></li>
<li id="wmd-code-button" class="wmd-button" style="left: 125px;"><span style="background-position: -80px -20px;"></span></li>
<li id="wmd-image-button" class="wmd-button" style="left: 150px;"><span style="background-position: -100px -20px;"></span></li>
<li id="wmd-spacer2" class="wmd-spacer" style="left: 175px;"><span style="background-position: -120px -20px;"></span></li>
<li id="wmd-olist-button" class="wmd-button" style="left: 200px;"><span style="background-position: -120px -20px;"></span></li>
<li id="wmd-ulist-button" class="wmd-button" style="left: 225px;"><span style="background-position: -140px -20px;"></span></li>
<li id="wmd-heading-button" class="wmd-button" style="left: 250px;"><span style="background-position: -160px -20px;"></span></li>
<li id="wmd-hr-button" class="wmd-button" style="left: 275px;"><span style="background-position: -180px -20px;"></span></li>
<li id="wmd-spacer3" class="wmd-spacer" style="left: 300px;"><span style="background-position: -200px -20px;"></span></li>
<li id="wmd-undo-button" class="wmd-button" style="left: 325px;"><span style="background-position: -200px -20px;"></span></li>
<li id="wmd-redo-button" class="wmd-button" style="left: 350px;"><span style="background-position: -220px -20px;"></span></li>
</ul>
</div>
<textarea id="wmd-input" class="wmd-input" name="post-text" cols="92" rows="15" tabindex="101" data-min-length=""></textarea>
</div>
</div>
<div class="fl" style="margin-top: 8px; height:24px;"> </div>
<div id="draft-saved" class="draft-saved community-option fl" style="margin-top: 8px; height:24px; display:none;">draft saved</div>
<div id="draft-discarded" class="draft-discarded community-option fl" style="margin-top: 8px; height:24px; display:none;">draft discarded</div>
<div id="wmd-preview" class="wmd-preview"></div>
<div></div>
<div class="edit-block">
<input id="fkey" name="fkey" type="hidden" value="a98ac895ae44dffe99803060b762b991">
<input id="author" name="author" type="text">
</div>
</div>
<div style="position: relative;">
<div class="form-item new-post-login">
<div class="new-login-form">
<div class="new-login-left">
<h3>Sign up or <a id="login-link" href="/users/login?ssrc=question_page&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f43496920%2fangular-http-post-changing-date-to-utc-date%23new-answer">log in</a></h3>
<script>
StackExchange.ready(function() {
StackExchange.helpers.onClickDraftSave('#login-link');
});
</script>
<div class="preferred-login google-login">
<svg aria-hidden="true" class="svg-icon native iconGoogle" width="18" height="18" viewBox="0 0 18 18">
<g>
<path d="M16.51 8H8.98v3h4.3c-.18 1-.74 1.48-1.6 2.04v2.01h2.6a7.8 7.8 0 0 0 2.38-5.88c0-.57-.05-.66-.15-1.18z" fill="#4285F4"></path>
<path d="M8.98 17c2.16 0 3.97-.72 5.3-1.94l-2.6-2a4.8 4.8 0 0 1-7.18-2.54H1.83v2.07A8 8 0 0 0 8.98 17z" fill="#34A853"></path>
<path d="M4.5 10.52a4.8 4.8 0 0 1 0-3.04V5.41H1.83a8 8 0 0 0 0 7.18l2.67-2.07z" fill="#FBBC05"></path>
<path d="M8.98 4.18c1.17 0 2.23.4 3.06 1.2l2.3-2.3A8 8 0 0 0 1.83 5.4L4.5 7.49a4.77 4.77 0 0 1 4.48-3.3z" fill="#EA4335"></path>
</g>
</svg> Sign up using Google
</div>
<div class="preferred-login facebook-login">
<svg aria-hidden="true" class="svg-icon iconFacebook" width="18" height="18" viewBox="0 0 18 18">
<path
d="M1.88 1C1.4 1 1 1.4 1 1.88v14.24c0 .48.4.88.88.88h7.67v-6.2H7.46V8.4h2.09V6.61c0-2.07 1.26-3.2 3.1-3.2.88 0 1.64.07 1.87.1v2.16h-1.29c-1 0-1.19.48-1.19 1.18V8.4h2.39l-.31 2.42h-2.08V17h4.08c.48 0 .88-.4.88-.88V1.88c0-.48-.4-.88-.88-.88H1.88z"
fill="#3C5A96"></path>
</svg> Sign up using Facebook
</div>
<div class="preferred-login stackexchange-login">
<svg aria-hidden="true" class="svg-icon native iconLogoGlyphXSm" width="18" height="18" viewBox="0 0 18 18">
<g>
<path fill="#BCBBBB" d="M14 16v-5h2v7H2v-7h2v5z"></path>
<path d="M5 15v-2h8v2H5zM12.09.72l4.5 6.06-1.2.9-4.51-6.06 1.21-.9zM8.34 4.3l.97-1.16 5.8 4.83-.96 1.16-5.8-4.83zm-1.9 3.36l.64-1.37 6.85 3.19-.63 1.37-6.85-3.2zM5.4 11.39l.38-1.67 7.42 1.48-.22 1.46-7.58-1.27z" fill="#F48024"></path>
</g>
</svg> Sign up using Email and Password
</div>
</div>
<input type="hidden" name="manual-openid" class="manual-openid">
<input type="hidden" name="use-facebook" class="use-facebook" value="false">
<input type="hidden" name="use-google" class="use-google" value="false">
<input type="button" class="submit-openid" value="Submit" style="display:none">
<div class="new-login-right">
<h3>Post as a guest</h3>
<div class="form-item">
<table>
<tbody>
<tr>
<td class="vm">
<div>
<label for="display-name">Name</label>
<input id="display-name" name="display-name" type="text" size="30" maxlength="30" value="" tabindex="105">
</div>
<div>
<label for="m-address">Email</label>
<input id="m-address" name="m-address" type="email" size="30" maxlength="100" value="" tabindex="106" placeholder="required, but never shown">
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
StackExchange.ready(function() {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43496920%2fangular-http-post-changing-date-to-utc-date%23new-answer', 'question_page');
});
</script>
<noscript>
<h3>Post as a guest</h3>
<div class="form-item">
<table>
<tr>
<td class="vm">
<div>
<label for="display-name">Name</label>
<input id="display-name" name="display-name" type="text" size="30" maxlength="30" value="" tabindex="105">
</div>
<div>
<label for="m-address">Email</label>
<input id="m-address" name="m-address" type="email" size="30" maxlength="100" value="" tabindex="106" placeholder="required, but never shown" />
</div>
</td>
</tr>
</table>
</div>
</noscript>
</div>
<div class="form-submit cbt">
<input id="submit-button" type="submit" value="Post Your Answer" tabindex="110">
<a href="#" class="btn-clear discard-answer dno">discard</a>
<p class="privacy-policy-agreement"> By posting your answer, you agree to the <a href="https://stackexchange.com/legal/privacy-policy" name="privacy" target="_blank">privacy policy</a> and
<a href="https://stackexchange.com/legal/terms-of-service" name="tos" target="_blank">terms of service</a>.</p>
<input type="hidden" name="legalLinksShown" value="1">
</div>
</form>
Text Content
Stack Overflow 1. Questions 2. Developer Jobs 3. Tags 4. Users CURRENT COMMUNITY * help chat Stack Overflow * Meta Stack Overflow YOUR COMMUNITIES Sign up or log in to customize your list. MORE STACK EXCHANGE COMMUNITIES company blog * Tour Start here for a quick overview of the site * Help Center Detailed answers to any questions you might have * Meta Discuss the workings and policies of this site * About Us Learn more about Stack Overflow the company * Business Learn more about hiring developers or posting ads with us 1. 2. 3. 4. Log In Sign Up Join Stack Overflow to learn, share knowledge, and build your career. Email Sign Up or sign in with Google Facebook ANGULAR $HTTP.POST CHANGING DATE TO UTC DATE Ask Question up vote 15 down vote favorite I was trying to post some data to my REST api which has date. Now while I debug, my date parameter is a JS Date object with correct date in my timezone: Tue Apr 04 2017 00:00:00 GMT+0530 after it leaves my code, and I see the same in network tab, it is converted to UTC date: "2017-04-03T18:30:00.000Z" I searched for the solution according to which I need to include locale file of angular in my index.html which I did: <script type="text/javascript" src="resources/js/angular/angular-locale_en-in.js"></script> but it doesn't help. I've seen solutions like adding date format to filter or something, but I want a global solution. Any help? Thanks :) javascript angularjs internationalization timezone share|improve this question asked Apr 19 at 13:17 pranavjindal999 682519 1 Not clear what specific problem is or what you are trying to change – charlietfl Apr 19 at 13:22 1 I'm selecting 4th April on my UI and server is getting 3 April in UTC. my rest apis are third party and can not change them. I want to handle this issue globally – pranavjindal999 Apr 19 at 13:26 1 but when you convert utc back to local will be the same .. 4th april. Still not clear what objective is – charlietfl Apr 19 at 13:28 1 that is what I'm saying, I cannot ask api to convert utc date to local timezone. they want date in local timezone already. – pranavjindal999 Apr 19 at 13:29 2 or modify Date.prototype.toJSON() which is what creates that string. But modifying native api is not a good practice – charlietfl Apr 19 at 14:16 | show 6 more comments 7 ANSWERS 7 active oldest votes up vote 6 down vote +25 Handling date, time, and timezone have confused me too. May be this answer gives you some insight on how you can handle them. Try the following code in Chrome's developer console and see how same date is presented in different formats: var date = new Date(); date.toISOString(); // "2017-04-29T09:54:28.714Z" date.toGMTString(); //"Sat, 29 Apr 2017 09:54:28 GMT" date.toLocalString(); //"4/29/2017, 3:24:28 PM" Any date that you create on client always records the date at zero timezone offset i.e. UTC+/-00:00 Z. For simplicity you may think UTC and GMT as same. When it comes to display purpose the same date is presented as per the browser's timezone. If you do console.log (date) it'll output Sat Apr 29 2017 15:24:28 GMT+0530 (IST) but that doesn't mean that the internal recording of the date is as per browser's timezone. It's just presented on screen/console as per browser's timezone. Look at date representations not as being converted from one timezone to another but look at them as different representation of the same date. In your browser it is represented as GMT+0530 offset and when it is sent to server it is the same date at zero timezone offset. As per your comment, if you choose 4th Apr at 00:00 AM in GMT+0530 timezone, internally it'll be 3rd Apr at 18:30 PM in at GMT+0 i.e. zero timezone offset. Let it go to server as it is. When you need to use this date, it comes back from server as 3rd Apr and it'll be displayed in browser as per the browser's timezone offset. There is no conversion involved, it is one date with different representation. I once asked a related question, may be this adds more clarification. And overall, this answer is still same as @geminiousgoel and @charlietfl answers. share|improve this answer edited May 23 at 12:18 Community♦ 11 answered Apr 30 at 10:47 Ritesh Jagga 511318 add a comment | up vote 4 down vote Passing UTC date to server is desired behavior. The client APIs are supposed to handle UTC time instead of assuming the dates are all local dates. But anyways, you can convert the date to string based on local time zone, and pass the string to server. share|improve this answer answered May 1 at 18:40 Dada 513 Good idea @Dada – Anas May 17 at 19:38 add a comment | up vote 3 down vote Like charlietfl suggested, probably the global hack would be to override Date.prototype.toJSON() method, but that's not a good practice. Where are you using your $http.post call? The best place to submit an $http request would be in a service. If you use a service, then I suggest you to enwrap your public service API, so that you could have "public" and "private" methods: these could be utilities to perform common operations, such as data transformations, validations.. angular.service('bookStoreService', ['$http'], function($http) { var normalizeBooks = function(booksArray){ booksArray.forEach(function(book){ // do something on the book }); }; var updateBooks = function(books){ normalizeBooks(books); $http.post('myurl', books); }; return { updateBooks : updateBooks }; }); share|improve this answer edited Apr 27 at 11:07 answered Apr 27 at 10:57 Redd Sanso 414 1 that is what I don't want to do.. I simply want angular to not do these transformations while posting. – pranavjindal999 Apr 29 at 5:29 1 Then, an $http interceptor is your solution – Redd Sanso Apr 30 at 18:55 Read the section "Transforming requests and responsese" here: docs.angularjs.org/api/ng/service/$http – Redd Sanso May 10 at 21:54 add a comment | up vote 3 down vote Scenario : Send date from UI into API call as an epoch time (UNIX Time) instead of date string. You can use getTime() method to convert the date into epoch time. var dateStr = "Tue Apr 04 2017 00:00:00 GMT+0530"; var dateEpoch = new Date(dateStr).getTime(); console.log(dateEpoch); // 1491244200000 (Local Time) Run code snippetHide results Expand snippet At receiver end, they have to convert this epoch time (UNIX time) into Date again.It will give the same local date\time that pass from the UI. Sample screenshot share|improve this answer answered May 1 at 16:24 Rohit Jindal 6,42553159 thank you for your answer, but that did not help. I've no control over API. – pranavjindal999 May 1 at 16:35 I am talking that the API team should handle at their end. – Rohit Jindal May 1 at 16:36 yes that is what I'm saying.. API team is third party.. I do not have control over API.. I somehow need a global solution so that the date I select is the date sent at server – pranavjindal999 May 1 at 16:38 Then how you will get the call back with some result. API team have to send right ?. they have to make some changes at their end as well – Rohit Jindal May 1 at 16:42 add a comment | up vote 3 down vote i think you just can pass it as string (if the api you use accept strings) with the format you need, let say "Tue Apr 04 2017 00:00:00 GMT+0530" and save it in back-end as string and then when you retrieve it, it will be string and so it will not be changed in any way. share|improve this answer answered May 2 at 7:25 Ahmad Abo Saa 1687 add a comment | up vote 2 down vote Jindal saab, It will work like this. When we select any date with date picker or just pass any value it takes the original local date but when we pass that value further it converts it into UTC, thereafter it needs to convert to local zone again at receiving end. Database saves date-time in UTC format. share|improve this answer answered Apr 27 at 8:54 geminiousgoel 3101414 add a comment | up vote 2 down vote Did you added the angular-locale_en-in.js library to your app? Something like this.... angular.module('myAngularApp', [ 'ngLocale']) Otherwise, the js library won't have any effect in your angular application. share|improve this answer answered Apr 28 at 18:38 Gi1ber7 198211 yes I did injected the same – pranavjindal999 Apr 29 at 5:30 add a comment | YOUR ANSWER * * * * * * * * * * * * * * * draft saved draft discarded SIGN UP OR LOG IN Sign up using Google Sign up using Facebook Sign up using Email and Password POST AS A GUEST Name Email <h3>Post as a guest</h3> <div class="form-item"> <table> <tr> <td class="vm"> <div> <label for="display-name">Name</label> <input id="display-name" name="display-name" type="text" size="30" maxlength="30" value="" tabindex="105"> </div> <div> <label for="m-address">Email</label> <input id="m-address" name="m-address" type="email" size="30" maxlength="100" value="" tabindex="106" placeholder="required, but never shown" /> </div> </td> </tr> </table> </div> discard By posting your answer, you agree to the privacy policy and terms of service. NOT THE ANSWER YOU'RE LOOKING FOR? BROWSE OTHER QUESTIONS TAGGED JAVASCRIPT ANGULARJS INTERNATIONALIZATION TIMEZONE OR ASK YOUR OWN QUESTION. asked 8 months ago viewed 1,728 times active 7 months ago ADMINISTRATION INTELLIGENCE AG Government * Front-end Web Developer (m/w) im eGovernment Würzburg, Deutschland€45k - €50k javascriptscrum * Java SE/EE Developer (m/w) im eGovernment Würzburg, Deutschland€45k - €55k javajava-ee * Software Tester (m/w) im eGovernment Würzburg, Deutschland€40k - €45k automated-testsscrum View all 3 job openings! FOOT LOCKER EUROPE .COM eCommerce, Fashion * JavaScript FrontEnd Engineer Recklinghausen, Germany javascriptangularjs View all job openings! Get the weekly newsletter! In it, you'll get: * The week's top questions and answers * Important community announcements * Questions that need answers see an example newsletter By subscribing, you agree to the privacy policy and terms of service. LINKED 1 What is the difference between Date.now and Date.getTime in Javascript and how timezone affects them? RELATED 2082 Change an element's class with JavaScript 303 How do you create a JavaScript Date object with a set timezone without using a string representation 1 Flex Date issue with Timezone 1 UTC seconds for the current date should give the UTC date when it is again converted to Date using these UTC seconds . 2 How to update the date to the current date for UTC -8 950 What is the difference between angular-route and angular-ui-router? -1 how to get date with JSON.stringify(data) in angularjs 1 new Date(year, month, date) in node js is automatically getting converted to UTC timezone 0 JSON.stringify serialising date incorrectly -1 UTC DateTime Conversion To US Eastern HOT NETWORK QUESTIONS * Releasing a T-Rex into a modern ecosystem wouldn't be that bad, right? * Simple Math Problem 4 * If a company that doesn't pay dividends makes a large profit, where does it go? * Connecting TVS before or behind Ethernet transformer? * Is "Assistant Professor Position (Tenure Track) for a female Researcher" illegal in Austria? * How to measure volume in raster? * In Star Trek III, why does David not regenerate like Spock? * Access to scientific articles that cite mine after leaving academia * A student forgot to answer an exam question * How do airports differentiate supplements from drugs? * How to convince my parents I'm happy with my social life? * Why (historically) include the number of arguments (argc) as a parameter of main? * How to halve the damage on a succesful saving throw? * Binary Sequences * How would a treaty banning specific weapons be enforced? * Letter writer snapped at my reminder email. What can I do in the future to prevent this, and should I still accept his letter? * Dismissing a stranger asking for help without being dismissive of their possible situation? * Why is the hypotenuse in trig always positive regardless of the quadrant? * antonym for "thimble" * Which worse thing was Luke expecting in Star Wars: A New Hope? * Sample the Pareto Distribution * Getting output from dbms_output.get_lines using JDBC * What type of helicopter is shown in Star Wars The Last Jedi featurette? * How to deal with sniping spellcaster PCs? question feed <div><img src="/posts/43496920/ivc/8e36" class="dno" alt="" width="0" height="0"></div> default STACK OVERFLOW * Questions * Jobs * Developer Jobs Directory * Salary Calculator * Help * Mobile STACK OVERFLOW BUSINESS * Talent * Ads * Enterprise COMPANY * About * Press * Work Here * Legal * Privacy Policy * Contact Us STACK EXCHANGE NETWORK * Technology * Life / Arts * Culture / Recreation * Science * Other * Stack Overflow * Server Fault * Super User * Web Applications * Ask Ubuntu * Webmasters * Game Development * TeX - LaTeX * Software Engineering * Unix & Linux * Ask Different (Apple) * WordPress Development * Geographic Information Systems * Electrical Engineering * Android Enthusiasts * Information Security * Database Administrators * Drupal Answers * SharePoint * User Experience * Mathematica * Salesforce * ExpressionEngine® Answers * Stack Overflow em Português * Blender * Network Engineering * Cryptography * Code Review * Magento * Software Recommendations * Signal Processing * Emacs * Raspberry Pi * Stack Overflow на русском * Programming Puzzles & Code Golf * Stack Overflow en español * Ethereum * Data Science * Arduino * Bitcoin * more (26) * Photography * Science Fiction & Fantasy * Graphic Design * Movies & TV * Music: Practice & Theory * Worldbuilding * Seasoned Advice (cooking) * Home Improvement * Personal Finance & Money * Academia * Law * more (16) * English Language & Usage * Skeptics * Mi Yodeya (Judaism) * Travel * Christianity * English Language Learners * Japanese Language * Arqade (gaming) * Bicycles * Role-playing Games * Anime & Manga * Puzzling * Motor Vehicle Maintenance & Repair * more (32) * MathOverflow * Mathematics * Cross Validated (stats) * Theoretical Computer Science * Physics * Chemistry * Biology * Computer Science * Philosophy * more (10) * Meta Stack Exchange * Stack Apps * API * Data * Area 51 * Blog * Facebook * Twitter * LinkedIn site design / logo © 2017 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2017.12.14.28170 <div id="noscript-warning">Stack Overflow works best with JavaScript enabled <img src="https://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" alt="" class="dno"> </div>