I used PHP code as follows to print MYSQL date value on html page. I want to do the same formatting using Jquery . How to do it?
in PHP
echo date("j F Y, g:i A",strtotime($add->added_date))
J query alert
var date = retdata[i].added_date; // date comes as 2016-02-27 06:14:15
alert (''); //formatting here??? i want as 2016 Feb 27 06:14 AM
You should use Moment.js to manipulate and parse dates in javascript.
If your date comes as a string, you should be able to parse in your format so (remember to include moment.js in your document and be careful with the format, the following is just an example and you should look for the time format of your date am,pm,24h):
var parsedDate = moment("2016-02-27 06:14:15","YYYY-MM-DD H m s");
console.log(parsedDate.format("YYYY MMM DD hh:mm A")); // 2016 Feb 27 06:14 AM
Try alert(date) since you defined date already.
You do not need to format the timestamp since you already formatted it using PHP. The only question (we cannot tell from the little bit of information you provided) is how you are passing the information to JavaScript/jQuery via the retdata array. This should show the "2016-02-27 06:14:15" (or whatever date/time information) from var date within the modal window if it is/was properly passed or defined before being called by alert().
Related
In my php application I set the italian timezone like this way:
date_default_timezone_set('Europe/Rome');
the string above is located in my config.php file, the core of the app. Anyway, from the backend I using momentjs with CodeIgniter framework. When a user select a date from the properly input set this result:
Now I get the value from this input like this:
var end_date_temp = Date.parse($('#end-datetime').val());
And the initial result is wrong:
Tue Mar 01 2016 11:03:00 GMT+0100 (ora solare Europa occidentale)
The rest of code is:
var end_date = moment(end_date_temp).add(serviceDuration, 'minutes').format('DD/MM/YYYY HH:mm');
$('#end-datetime').val(end_date);
NB: I also tried to set moment.locale('it') but the same result appear, in my javascript libraries I've the italian timezone of momentjs. What is wrong?
UPDATE Code:
var end_date_temp = moment($('#end-datetime').val())._i;
var end_date = moment(moment(end_date_temp).add(serviceDuration, 'minutes')).format('DD/MM/YYYY HH:mm');
$('#end-datetime').val(end_date);
// parse the input string to a moment object, **specifying the input format**
var end_date = moment($('#end-datetime').val(), 'DD/MM/YYYY HH:mm');
// manipulate it as desired
end_date.add(serviceDuration, 'minutes');
// format it to the specified output format, and assign the result back to your field
$('#end-datetime').val(end_date.format('DD/MM/YYYY HH:mm'));
You can do this in one line of code if you like.
$('#end-datetime').val(moment($('#end-datetime').val(), 'DD/MM/YYYY HH:mm').add(serviceDuration, 'minutes').format('DD/MM/YYYY HH:mm'));
The locale setting isn't important with this particular bit of code, because you don't use any locale-specific functions or format specifiers
After a lot of attempts, I fixed in my timezone (italian) like so:
moment.locale('it');
var end_date_temp = moment($('#end-datetime').val()).format('DD/MM/YYYY HH:mm')
var end_date = moment(end_date_temp).add(30, 'minutes');
$('#end-datetime').val(moment(moment(end_date).toDate()).format('DD/MM/YYYY HH:mm'));
I declared the .locale as it, in the next time I formatted the time returned from the input text in my timezone as well. I've manipulated the date with the .add method, for do this I've create another instance of momentjs object. At the end I pass the value from the input, re-formatted the date again in my timezone format 'cause in the manipulation I lose the previous formatting. The final result is what I wanted. Hope this help, anyway, if someone find a solution more optimized than my I'll be happy to see.
momentjs is using the american date format (MM/DD/YYYY), enforce a format to get the right date:
var input = '03/01/2016 11:00';
var date = moment(input, 'DD/MM/YYYY HH:mm');
document.write(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.0/moment.min.js"></script>
I want to display a GMT date time string to IST(GMT +0530) time format by using moment.js. I am assigning the PHP date time value to a javascript variable and then converting the GMT time to IST time using moment.tz().format(); method. But when I alert the converted value by specifying the format parameters ,the alert is showing the formatted time with chinese letters. moment.js is clearly confusing. Please help me solve this...
GMT Datetime value is "2014-11-28 20:15:26" which is i am getting from PHP variable
"2014-11-29T01:45:26+05:30" is value of Converted value of date time variable to IST by using moment.tz(m,zone).format();
"11月 29日 2014 1:45 早上" is the value of converted value with format parameters moment.tz(m,zone).format('MMM Do YYYY h:mm a');
My code is
var start_dates = '<?php echo $times_start; ?>';
var zone = "Asia/Kolkata";
var m = moment.tz(start_dates,'Europe/London').format();
var time = moment.tz(m,zone).format('MMM Do YYYY h:mm a');
The only way you could get Chinese characters is if you have set the Chinese locale with either the lang or locale functions. You might have done that somewhere else in your script.
With regard to the code you wrote, that interprets the input as the time in London - which is not the same thing as GMT or UTC. (London alternates between GMT and BST for daylight saving time.)
You also don't need to format it just to parse it again.
You just need to do this:
var m = moment.utc(start_dates);
var time = m.tz(zone).format('MMM Do YYYY h:mm a');
This will work for any of the supported zones. But if you know for a fact that you will always be converting to Indian Standard Time, since it doesn't use DST, you don't really need moment-timezone. You can just do this:
var m = moment.utc(start_dates);
var time = m.utcOffset("+05:30").format('MMM Do YYYY h:mm a');
JSON Date: '/Date(1373428800000)/'
End Result: 7/9/2013 8:00 PM EST
Currently I do it in 3 steps:
var a = cleanJsonDate('JsonDate');
var b = formatDate(a); // 7/10/2013 12:00 AM
var c = moment.utc(b); // 7/9/2013 8:00 PM
return c;
Is it possible to accomplish the same result using moment js only?
----Update-----
Combining #ThisClark & #Matt answers. I came as close as possible to the goal; however, the 'h' format does not work for some reason, I still get 20.00.00 instead of 8:00
var m = moment.utc(moment('/Date(1373428800000)/').format('M/D/YYYY h:m A')).toDate();
alert(m);
<script src="http://momentjs.com/downloads/moment.min.js"></script>
This format is already supported natively by moment.js. Just pass it directly.
moment('/Date(1373428800000)/')
You can then use any of the moment functions, such as .format() or .toDate()
If you want UTC, then do:
moment.utc('/Date(1373428800000)/')
Again, you can call format or toDate, however be aware that toDate will produce a Date object, which will still have local time behaviors. Unless you absolutely need a Date object, then you should stick with format and other moment functions.
I don't see all your code, but if you can just get the value of milliseconds as 1373428800000 out of that json, then you can pass it to moment directly. I think formatDate is a function you wrote. Does it do something important like manipulate time that you require of moment.js, or could you just use the format function of moment?
var date = 1373428800000;
var m = moment.utc(date);
//var m = moment.utc(date).format('M/D/YYYY H:mm A'); <-- alternative format
alert(m);
<script src="http://momentjs.com/downloads/moment.min.js"></script>
In database i have a row with date & time, say 2014-04-16 00:00:00 then I convert that datetime to unix timestamp using
strtotime('2014-04-16 00:00:00') * 1000; // result 1397577600000
In javascript i am trying to get the hour using the following code
var d = new Date(1397577600000); // 1397577600000 from php unix timestamp in previous code
d.getHours(); // return 23
d.getMinutes(); // return 0
why getHours() return 23 instead of 0? is there any difference between js timestamp and php timestamp?
Date objects in javascript will always return values based on the browser's current timezone. So if d.getHours() is returning 23 for you, that would suggest your local browser timezone is one hour earlier than UTC (-01:00).
It you want the hours for a Date object based on UTC timezone, you can use:
d.getUTCHours()
Follow Up:
Just to throw out some free advice, you could use the following code to deal with your date values from one context to another:
PHP:
// Fetched from the db somehow
$datetime_db = '2014-04-16 00:00:00';
// Convert to PHP DateTime object:
$datetime_obj = new DateTime($datetime_db, new DateTimeZone('UTC'));
// Format DateTime object to javascript-friendly ISO-8601 format:
$datetime_iso = $datetime_obj->format(DateTime::W3C);
Javascript:
var d = new Date('2014-04-16T00:00:00+00:00'); // 2014-04-16T00:00:00+00:00 from PHP in previous code
d.getUTCHours(); // returns 0
This keeps the datetime variables in a language-specific object format when being handled in that language, and serializes the value into a string format that all current browsers/languages accept (the international standard ISO-8601).
I am getting 21 here because in Javascript local timezone of the user will be considered to fetch time and date.
Ok, based on what Arun P Johnny said.
I change the strtotime parameter to match my timezone, in this case i changed it to
strtotime('2014-04-16 00:00:00 GMT+7') * 1000;
hope this help anybody that have the same problem as I.
I have a website and i want to display the PHP generated date through JavaScript and jQuery. The problem is, when i save the date string returned by PHP Date("d-m-Y H:i:s") function and pass this string to JavaScript new Date() function, it displays the next year rather than the current. For example, right now the year is 2013, but is shows 2014 instead. Below is my code to better explain my problem.
$(document).ready(function(){
var phpDateString = "<?php echo Date("d-m-Y H:i:s");?>"
alert(phpDateString); // Displays: 13-12-2013 01:49:17 (this is correct date)
var date = new Date(Date.parse(phpDateString));
alert(date); //Displays: Sun Jan 12 2014 01:53:20 (this is incorrect date)
});
I am using localhost Xampp Server and i have changed its timezone to date.timezone=Asia/Karachi in php.ini file. Why this is happening. Would be great if someone helps me out.
JavaScript is expecting MM-DD-YYYY but you are giving it DD-MM-YYYY. So it is turning 13 into 1 and thus you get January instead of December and an additional year being added. Because today is 12/12 it is a coincidence that the day is correct otherwise that would be wrong, too.