Birthdate off by one date - javascript

I have a javascript app in which the user selects their birthdate using a 3rd party datepicker.
This date is then sent to the server, where it is stored in SqlServer as a Date (not a DateTime, nor a DateTimeOffset).
This typically works fine. However, if the user is in a timezone such as +2:00, and it's just after midnight, then the date which is sent to the server is now different.
For example, I select the date of Jan 1, 2000 in the DatePicker.
The value which is being sent to the server is: 1999-12-31T22:00:00.000Z
The server then strips the time off it to store it as a date, and then the date is now off by one.
How do I resolve this?

I believe that dateFormat: "d/MM/yyyy" option of datepicker is what you're looking for.
Otherwise you can use the following code to convert Date object into the required string.
date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()
Both of the options will give you a string with the date, which you can then convert to date object in sql using str_to_date
See: https://stackoverflow.com/a/1861519/2652134

Related

How to localize input from an html datetime-local input

I used a standard html datetime-local input like <input type="datetime-local" />, I put in a time of February 16th, 6 pm from my pc in the EST GMT-5 timezone. From another pc in the same timezone I display the date stored in DB from the input, but it shows February 16th, 1 pm instead of 6 pm. I know this is a timezone problem because there is a 5 hour difference and im in GMT-5. How would I convert the users input to UTC time based on their local time, then store it the db as a UTC time?
EDIT: I did new Date(...).toUTCString() on my server instead of on my client, does this have anything to do with the timezone problem?
(1) the data we get from datetime-local input is like this
const startTime = "2021-08-28T09:00"
There is no timezone.
The MDN link here:
datetime-local
(2):
When we send the data to the server (the server timezone may different in different environment) and save in the DB(normally always in UTC timezone) we need to make sure we send the right date time. Convert it to UTC time before sending to server.
We can do:
let result = new Date(startTime);
result.toISOString();
It converts to UTC. Then it will save in the DB as UTC.
Link here: toISOString()
(3):
We get the UTC data from DB, when it display on the FrontEnd, we need to convert to our local timezone and right format as below.
this.startTime = moment(this.startTime).format("YYYY-MM-DDTHH:mm");
Info here https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local
<input type="hidden" id="timezone" name="timezone" value="-05:00">

Javascript - displaying locale time from mySQL TimeDate

I am having an issue with displaying the correct time. I have a php script that when a button is clicked it inserts the CURRENT_TIMESTAMP into the database. The server is located in Arizona, I am in PST. When I call the time in my script it shows Arizona time, but I need it to show the users time. So 2015-02-18 16:06:28 Arizona time, MY time is 2015-02-18 15:06:28.
How do i get the correct time. I am using moment.js, but no matter how i format it it shows the incorrect time. I am not sure but is DST, not being considered?
var time_in = time_in;//format 2015-02-18 16:17:33
var timeIn = moment.utc(time_in, "HH:mm a").format("HH:mm a");
Moment.js parses the date as a locale date-time. So when you do moment.utc(time_in), you're converting it to UTC according to your local time (PST), shifted forward or backwards.
So what you need to do is do a moment.fn.utcOffset. Arizona is UTC-07:00, so we would want to add +7 to the offset. You can do the same using moment.fn.zone but that's getting deprecated.
var utcTime = moment.utc('2015-02-18 16:06:28').utcOffset(+7).format('YYYY-MM-DD HH:mm:ss')
// returns "2015-02-18 23:06:28" which is the UTC time
Now you have the moment in UTC, you can convert it to the client localtime:
moment(moment.utc(utcTime).toDate()).format('YYYY-MM-DD HH:mm:ss')
// returns '2015-02-18 15:06:28' (which is PST)
moment.utc(utcTime).toDate() above just converts the utc time to your local time, then formatting it with momentjs
EDIT: If possible, you should use unix timestamp when sending to server, then you don't have to deal with UTC or timezones. You can convert to local time with moment.unix(unixTimestamp).format('YYYY-MM-DD HH:mm:ss')
It looks like you are using Javascript to get the time of the client, but then not passing that to the PHP. I'm not sure how your app is structured, but you could create an input tag with the type="hidden". Then using Javascript, find the element and set it's value to Date().
Here is an example: http://jsfiddle.net/43jfefuq/
Now when you submit this form with PHP, the value in the field will be the client's local time.

NetSuite RESTlet, Submitting Date/Time Timezone Issues

I have a RESTlet for creating new customer entities from form submissions on our website. I'm trying to save three values in custom Date/Time entity fields that I added to the customer record. The service accepts the dates as timestamp integers (e.g. 1421434991537). Then the RESTlet code needs to submit that timestamp as the correct date in Eastern Time.
I've been reading up on the issues with NetSuite date/times, where when I create the date object using "new Date(myIntVal)", it creates it in Pacific Time. I've been trying to pass the Olson value into setDateTimeValue, like the documentation and some other blog posts say.
I need these values saved as Eastern Time, but no matter what I do, the time is ALWAYS submitted in Pacific Time. What am I missing?
// firstVisit, previousVisit, and currentVisit are all the timestamp int values.
var firstVisitDate = nlapiDateToString(new Date(firstVisit), 'datetimetz');
var previousVisitDate = nlapiDateToString(new Date(previousVisit), 'datetimetz');
var currentVisitDate = nlapiDateToString(new Date(currentVisit), 'datetimetz');
leadRecord.setDateTimeValue(FIELD_NAME_WEB_FIRST_VISIT, firstVisitDate, 14);
leadRecord.setDateTimeValue(FIELD_NAME_WEB_PREVIOUS_VISIT, previousVisitDate, 14);
leadRecord.setDateTimeValue(FIELD_NAME_WEB_CURRENT_VISIT, currentVisitDate, 14);
The nlapiSetDateTimeValue API has the following parameters fieldId, dateTime and timeZone. The timeZone parameter is not the timezone to which the date should be converted to. It should be the timezone of the dateTime field. This API converts the value of dateTime to the USER'S TIMEZONE as set up in their NetSuite account.
So make sure that the user running the RESTlet has their timezone set to Eastern then pass the Olson value of Pacific in your API call.
NetSuite servers are set to have PST Date/Time. If your date/time is on EST, convert it first to PST before updating the record and if your are pulling out the date/time from NetSuite convert it to EST then.

angular.ui DatePicker how to set the Date format?

I am using the angular.ui DatePicker
http://angular-ui.github.io/bootstrap/#/datepicker
In the doc it says:
"Everything is formatted using the date filter and thus is also localized."
I am having a hard time figuring out how to use the date filter to control the date format set in the model.
Using the DatePicker I am getting a date format set in the model like this:
"2013-09-07T03:18:43.000Z"
However, I am trying to serialize with Grails, and I want my date format to be like this:
"2013-09-07T03:18:43Z"
How do I configure the DatePicker to output this format of date? Or since it is a JavaScript date, do I have to do this at the server side? Ultimately, I would like to send:
"2013-09-07T03:18:43Z"
in a JSON put to the server.
TIA
You can follow this trademark way of parsing the default ISO date format to your own format stripping out the milliseconds, in client side.
Or, in the server side (Grails) you can parse the string to your own format as below:
Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "2013-09-07T03:18:43.000Z")
.format("yyyy-MM-dd'T'HH:mm:ss'Z'")
//prints "2013-09-07T03:18:43Z"
If you are persisting a Date instead of a String, just use Date.parse(..) (without .format).

javascript jquery why is my formatted date substracting 1 one day from the actual value

I have a date that is stored in my sql server database as 2013-06-12 00:00:00.0000000. But when I retrieve it with $.getJSON and then format it, the date is displayed as jun 11 2013. As a check, I displayed the value with out any formatting to make sure the correct value would render, in which it did. Can anybody give me a clue as to why a day is being subtracted from the original date when formatted. Here is the jquery code that i am using:
$.datepicker.formatDate('M dd yy', new Date(val.DeliveryDate))
Your dates are stored as GMT in MySql. When you grab them through your getJSON call, the date is converted to your local time zone. It's already tomorrow in England relative to us (8pm).

Categories