I have an function to get forecast weather ajax to load this into div but the date in json
have a datetime format type: "dt":1452254400
I need format: 'Y-m-d'
but in ajax I can't use the gmdate()
php function to get this.
I'm trying get this with
$.ajax({
url: urlforecast,
dataType:"JSON",
success:function(data){
var str = (""+data['list'][0]['dt']);
var num = parseInt(str.replace(/[^0-9]/g, ""));
var date = new Date(num);
alert(date);
But he format is: Sat Jan 17 1970 20:29:56 GMT+0100
and I dislike this.
How can I get the format like gmdate()
into ajax
function?
You have seconds since epoch, javascript uses milliseconds, so you have to multiply by a thousand
$.ajax({
url : urlforecast,
dataType : "JSON",
success : function(data){
var num = (+data['list'][0]['dt']) * 1000;
var date = new Date(num);
}
});
To get Y-m-d
you'd do
var date = new Date(num);
var ymd = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
Try multiplying num
by 1000
, using .toJSON()
, String.prototype.slice()
var num = 1452254400;
var ymd = new Date(num * 1000).toJSON().slice(0, 10);
console.log(ymd)
</div>