Last active
May 31, 2017 16:32
-
-
Save ryanburnette/8803238 to your computer and use it in GitHub Desktop.
A jQuery method that sets datetime fields to the current UTC time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.setNow = function (onlyBlank) { | |
var now = new Date($.now()); | |
var year = now.getFullYear(); | |
var month = (now.getMonth() + 1).toString().length === 1 ? '0' + (now.getMonth() + 1).toString() : now.getMonth() + 1; | |
var date = now.getDate().toString().length === 1 ? '0' + (now.getDate()).toString() : now.getDate(); | |
var hours = now.getHours().toString().length === 1 ? '0' + now.getHours().toString() : now.getHours(); | |
var minutes = now.getMinutes().toString().length === 1 ? '0' + now.getMinutes().toString() : now.getMinutes(); | |
var seconds = now.getSeconds().toString().length === 1 ? '0' + now.getSeconds().toString() : now.getSeconds(); | |
var formattedDateTime = year + '-' + month + '-' + date + 'T' + hours + ':' + minutes + ':' + seconds; | |
if ( onlyBlank === true && $(this).val() ) { | |
return this; | |
} | |
$(this).val(formattedDateTime); | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made the change to
month
suggested by @eayavuz.