diff --git a/js/customize-post-date-control.js b/js/customize-post-date-control.js index 6d49692..2807226 100644 --- a/js/customize-post-date-control.js +++ b/js/customize-post-date-control.js @@ -183,8 +183,8 @@ return; } - remainingTime = ( new Date( control.setting.get().post_date ) ).valueOf(); - remainingTime -= ( new Date( api.Posts.getCurrentTime() ) ).valueOf(); + remainingTime = api.Posts.parsePostDate( control.setting.get().post_date ).valueOf(); + remainingTime -= api.Posts.parsePostDate( api.Posts.getCurrentTime() ).valueOf(); remainingTime = Math.ceil( remainingTime / 1000 ); if ( remainingTime > 0 ) { control.scheduledCountdownContainer.text( control.scheduledCountdownTemplate( { remainingTime: remainingTime } ) ); diff --git a/js/customize-post-status-control.js b/js/customize-post-status-control.js index fe582e1..562f39f 100644 --- a/js/customize-post-status-control.js +++ b/js/customize-post-status-control.js @@ -110,8 +110,8 @@ */ updateChoices: function updateChoices() { var control = this, data = control.setting.get(), isFuture, postTimestamp, currentTimestamp; - postTimestamp = ( new Date( data.post_date ) ).valueOf(); - currentTimestamp = ( new Date( api.Posts.getCurrentTime() ) ).valueOf(); + postTimestamp = api.Posts.parsePostDate( data.post_date ); + currentTimestamp = api.Posts.parsePostDate( api.Posts.getCurrentTime() ); isFuture = postTimestamp > currentTimestamp; /* @@ -119,6 +119,8 @@ * when server time and client time aren't exactly aligned. If the * status is publish, and yet the post date is less than 15 seconds * into the future, consider it as not future. + * + * See also https://github.com/xwp/wp-customize-posts/issues/303 */ if ( isFuture && 'publish' === data.post_status && postTimestamp - currentTimestamp < 15 * 1000 ) { isFuture = false; diff --git a/js/customize-posts.js b/js/customize-posts.js index 09268f9..b2a3c8a 100644 --- a/js/customize-posts.js +++ b/js/customize-posts.js @@ -534,13 +534,26 @@ component.getCurrentTime = function getCurrentTime() { var currentDate, currentTimestamp, timestampDifferential; currentTimestamp = ( new Date() ).valueOf(); - currentDate = new Date( component.data.initialServerDate.replace( ' ', 'T' ) ); + currentDate = component.parsePostDate( component.data.initialServerDate ); timestampDifferential = currentTimestamp - component.data.initialClientTimestamp; timestampDifferential += component.data.initialClientTimestamp - component.data.initialServerTimestamp; currentDate.setTime( currentDate.getTime() + timestampDifferential ); return component.formatDate( currentDate ); }; + /** + * Parse post date string in YYYY-MM-DD HH:MM:SS format (local timezone). + * + * @param {string} postDate Post date string. + * @returns {Date} Parsed date. + */ + component.parsePostDate = function parsePostDate( postDate ) { + var dateParts = _.map( postDate.split( /\D/ ), function( datePart ) { + return parseInt( datePart, 10 ); + } ); + return new Date( dateParts[0], dateParts[1] - 1, dateParts[2], dateParts[3], dateParts[4], dateParts[5] ); // eslint-disable-line no-magic-numbers + }; + /** * Focus on the control requested from the preview. *