2

I have a date that is set in my UI from this angularjs date picker. It is set as a moment.js object and it includes a GMT offset for my timezone (Eastern). When I submit the moment object back to my Asp.net Core endpoint, the time is converted to the time with no offset. So for example, when I inspect date in the javascript moment object object:

timeIn: Fri Dec 16 2016 05:00:00 GMT-0500 (Eastern Standard Time)

And when I inspect my object in .net core:

TimeIn: 12/16/2016 10:00:00AM

I want the data on the back end to show as the exact time entered on the UI, regardless of timezone. So if they enter 5:00AM, the time backend should store is 5:00AM. It appears to change the value when the moment object is converted to JSON to send to the backend. When I do a JSON.stringify() of my timeIn object on the UI, I get:

"2016-12-16T10:00:00.000Z"

Is there a way to strip the offset data from the timezone on the front end so that the time entered is the exact same as the time sent?

P.S. I know there are a lot of questions out there about time but I wasn't able to find a suitable one for my case. Thanks for your help

EDIT: I am using .net DateTime object to accept the javascript date object.

big_water
  • 3,024
  • 2
  • 26
  • 44
  • If you switch to a DateTimeOffset your life will become easier. – GlennSills Dec 17 '16 at 23:41
  • I believe DateTimeOffset identifies a single point in time so it will take timezones into account. For example, if a user enters timeIn of 5am and they are in the pacific timezone, and then a supervisor in the eastern timezone looks at that time entry, my app *should* show 5am to the supervisor, not 8am. I need the time entered to show the same to any user, regardless of the timezone they are in. – big_water Dec 20 '16 at 13:28

1 Answers1

0

This format contains 5 spaces:

Fri Dec 16 2016 05:00:00 GMT-0500 (Eastern Standard Time)

And we can assume 20 to be a minimum, safe entry for landing in the year. Get the next occurrence of space character and split from there.

Note: This one is totally based on assumptions, but it looks like it works better.

You can do something like:

var time = "Fri Dec 16 2016 05:00:00 GMT-0500 (Eastern Standard Time)";
console.log(time.substr(0, time.indexOf(" ", 20)));

Also, with reference to How to get the nth occurrence in a string?, we can achieve something better.

function getPosition(string, subString, index) {
   return string.split(subString, index).join(subString).length;
}

We need the 5th occurrence of the space:

function getPosition(string, subString, index) {
   return string.split(subString, index).join(subString).length;
}
var time = "Fri Dec 16 2016 05:00:00 GMT-0500 (Eastern Standard Time)";
console.log(time.substr(0, getPosition(time, " ", 5)));

The second method seems to be a better one than the first.

The third method is to use the Moment.js library to format the time into a format accepted by .Net:

timeIn = timeIn.format('MM-DD-YYYY h:mm:ss');
Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Looks like that does work. I found another way to do it using the moment.js library. timeIn = timeIn.format('MM-DD-YYYY h:mm:ss'); This format is accepted by .net and comes in properly as well. – big_water Dec 16 '16 at 22:43
  • @big_water Awesome... `:D` Do you wanna add that into my answer and accept it? Your wish... `:)` – Praveen Kumar Purushothaman Dec 16 '16 at 22:43
  • I will do so! Although I don't think the moment.js library will run in a snippet, unless you know of a way? – big_water Dec 16 '16 at 22:46
  • 1
    @big_water It runs mate. Okay, I'll add it myself then. – Praveen Kumar Purushothaman Dec 16 '16 at 22:47
  • No offense, but are you complete nuts to post such a solution? This is just plain wrong. The time sent to the server is the correct date, it's the UTC representation of `Fri Dec 16 2016 05:00:00 GMT-0500`, which is absolut correct. What you effectively did is, to cut off the timezone and the time send to the server is effectively 5 hours off (in this case) from the real time, which will completely mess off the dates. A person who posts something at 5:00 GMT-5 will appear "before" someone who post at 8:00 GMT -1, even though "8:00 GMT -1" is clearly 1 hour earlier than "5:00 GMT-5". – Tseng Dec 17 '16 at 10:37
  • This will completely screw the sorting and filtering by time, so if you don't understand UTC time, don't post anything on it. – Tseng Dec 17 '16 at 10:38
  • @Tseng You are absolutely right. This is for a localised application and I have spoken with the OP. So nothing to worry. `:)` – Praveen Kumar Purushothaman Dec 17 '16 at 12:45