简体   繁体   中英

unable to save the exact date in database

I have tried many times to store the exact date which I have selected. But when saving, it will take the previous date. I have tested in many ways, the angular file will pass the same selected date. When the sql query is executed, then it will take the incorrect date. can anyone please help me to solve this?

Below is my code. html code:

     <td class="data_field">
         <input class="form-control" type="date" name="date_main_domain" ng-model="domain.date_main_domain" id="date_main_domain" required value="{{domain.date_main_domain}}">
     </td>

sql query:

    $database->execute( "UPDATE domain_information SET date_main_domain='$dateMainDomain' WHERE id=$domainId" );

You need a valid date for the database to accept it, format your date object

        function formatDate(date) {
            var d = new Date(date),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear();

            if (month.length < 2) month = '0' + month;
            if (day.length < 2) day = '0' + day;

            return [year, month, day].join('-');
        }

        // date object to save
        var date = new Date(formatDate($scope.domain.date_main_domain));

Sounds like a timezone issue to me.

I guess on angular side the local timezone of the browser is taken.

Then you send it to the server. From my experience i would say it's beeing transfered as a utc time which represents the same time as the local time in the browser. So, for instance if the user is located in china at 12:00 (UTC+8), i guess 04:00 UTC-0 will be transfered.

At the server side the date will be set. Dependent on the methods you call on the date object you will see the utc-0 or the server timezone based representation.

You should analysis the date inside your request which goes to the server. If you just want the right day and don't care about the local timezone of the user you might concider to change the timezone of your date to Utc-0 on frontend side before you send it. (use a copy, so that you don't affect ypur model date)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM