Last active
August 29, 2015 14:26
-
-
Save pipiscrew/9e60472266a8c319ac9d to your computer and use it in GitHub Desktop.
UTC Calcs
This file contains hidden or 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
<!-- http://www.pipiscrew.com/2015/08/js-utc-calcs/ --> | |
<script> | |
$(function () { | |
//init with date now | |
$("[name=post_date]").val(date_formatter(new Date())); | |
//edit button - read record | |
function query_POSTS_modal(rec_id){ | |
$.ajax( | |
{ | |
url : "tab_posts_fetch.php", | |
type: "POST", | |
data : { post_id : rec_id }, | |
success:function(data, textStatus, jqXHR) | |
{ | |
if (data!='null') | |
{ | |
$('[name=post_date]').val(getGMTfromUTC(data.post_date)); | |
console.log(getGMTfromUTC(data.post_date)); | |
$('#lblTitle_POSTS').html("Edit Post"); | |
$('#modalPOSTS').modal('toggle'); | |
} | |
}, error: function(jqXHR, textStatus, errorThrown) {} | |
}); | |
} | |
//////////////////////////////////////// | |
// MODAL SUBMIT aka save | |
$('#formPOSTS').submit(function(e) { | |
e.preventDefault(); | |
var postData =form.serializeArray(); | |
var formURL = form.attr("action"); | |
var dt = parseDate($("[name=post_date]").val()); | |
var post_date_utc = getUTCfromGMT(dt); | |
//add date as UTC | |
postData.push({name: "post_date_utc", value : JSON.stringify(post_date_utc)}); | |
$.ajax( | |
{ | |
url : formURL, | |
type: "POST", | |
data : postData, | |
success:function(data, textStatus, jqXHR) | |
{ }, | |
error: function(jqXHR, textStatus, errorThrown) | |
{ } | |
}); | |
}); | |
}); //jQ | |
function date_formatter(value) { | |
if (value==null) | |
return ""; | |
var date = new Date(value); | |
var options = { | |
year: "numeric", month: "2-digit", | |
day: "2-digit", hour: "2-digit", minute: "2-digit", hour12: false | |
}; | |
return(date.toLocaleString("en-GB", options)); | |
} | |
function parseDate(dt_txt) { | |
dt_txt= dt_txt.replace('T',' '); | |
var data = dt_txt.split(' '); | |
var datePart = data[0].split('/'); | |
var timePart = data[1].split(':'); | |
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]]) | |
return (new Date(datePart[2], datePart[1] - 1, datePart[0], timePart[0], timePart[1])); | |
// months are 0-based | |
} | |
function getUTCfromGMT(val) { | |
var newDay = new Date(); | |
var t = new Date(val); | |
var time1 = Math.round(t / 1000); | |
return (time1); | |
} | |
function getGMTfromUTC(val) { | |
var d = new Date(); | |
d.setTime(val * 1000); | |
var hours = d.getHours(); | |
var minutes = d.getMinutes(); | |
if (hours < 10) { | |
hours = "0" + hours; | |
} | |
if (minutes < 10) { | |
minutes = "0" + minutes; | |
} | |
//http://www.w3schools.com/jsref/jsref_getmonth.asp | |
// months are 0-based | |
return d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear() + " " + hours + ":" + minutes; | |
} | |
</script> | |
<input type="text" name="post_date" class="form-control" placeholder="dd/mm/yyyy hh:ii"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment