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.
Related
My handlebars code is as followed.
{{bootstrap-datepicker value=from placeholder="MM/DD/YYYY"}}
Here the user picks a date from a calendar to get the from date. This is for a form.
My problem is the format is as followed: Sun May 29 2016 00:00:00 GMT-0700 (PDT)
and the post request requires milliseconds since epoch format.
SO
all the code sample I've seen to fix these problems, require the date in the function.
example:
var myDate="26-02-2012";
myDate=myDate.split("-");
var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
alert(new Date(newDate).getTime());
or
function toTimestamp(year,month,day,hour,minute,second){
var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
return datum.getTime()/1000;
}
or
var fromFormat = moment('from').fromFormat(moment(), 'milliseconds');
but I only have the name value as it changes all the time.
I've got moment-js, date-picker and a couple other add-ons. Any resources or code snippets will be appreciated.
So I finally figured it out with some help from friends.
I created an action that took the formatted date from date-picker and transformed it into UNIX time in milliseconds.
actions: {
PublishForm{
var fromDate = moment(from).valueOf();
}
}
**I've changed it this way due to issues displaying the date with the bootstrap-datepicker when you use the changeDate.
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().
Having searched endlessly for an answer, nothing has so far worked - so here I am hoping someone can assist.
I have 2 fields:
Action_Date & Deadline
The user updates the Action_Date field with the date they worked on the item. I then need to auto-populate the Deadline field with Action_Date + 14 days.
I have tried this so far:
function testCalc() {
var actionDate = new Date(document.getElementById("Action_Date").value);
var sla = 14;
var deadline = actionDate + sla;
document.getElementById("Deadline").value = deadline;
}
Action_Date calls this function via onblur()
The Deadline field auto-populates with the same date as Action_Date, in full format (e.g. Thu Jan 01 2015 00:00:00 GMT+0000(GMT Standard Time)14
Clearly the 14 days are not adding. Additionally I need Deadline to show the result in dd/mm/yy.
Any ideas most welcome!
Javascript dates are in milliseconds by default. So adding "14" to it is simply adding 14 milliseconds not 14 days. You will need to investigate more about Javascript Dates() here:
http://www.w3schools.com/jsref/jsref_obj_date.asp
Or use a framework of some kind like moments.js which is one of the best date handling frameworks out there. Date manipulation is one of the more complex things we developers have to do on the web due to issues with their local time, server time, etc.
I have selected the 12th of September 2014 in the UI.
Following is the code
ctrl.$formatters.push(function (modelValue) {
console.log(modelValue);
var dt = new Date(modelValue);
dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset());
console.log(dt)
return dt;
});
The two console logs i see are the following.
09/11/2014
Wed Sep 10 2014 18:30:00 GMT+0530 (IST)
Am not sure why the conversion from UTC to local is not carried out correctly.
Thanks in advance.
Its not clear what you are trying to do. The input does not have a time. Do you want to add the current time of the day to the arbitrary date? Or do you just want a local representation of the date? I'm geussing the latter.
Instead of dt.setMinutes(...) and the following two lines, replace all three lines with:
return dt.toLocaleDateString();
If you ARE trying to set the time to now on whatever date is input (I don't know why...) but you might try:
dt.setTime( new Date().getTime() );
instead of the setMinutes(...) line.
then you can
return dt.toLocaleString;
All date objects are stored as miliseconds since, like 1972. It is best to use the built in Date object methods to get what you want from it. Here are the docs for reference.
I understand that the datepicker (I'm using AngularStrap's datepicker) is "behind" a day because of how dates are calculated in javascript. My problem is, how to I get it to not take the timezone into consideration and just stick with the entered date... no adjustments?
I select February 1, 2014 in the datepicker. My value on the screen is 2/1/2014 and I want that value to be saved. However, the datepicker turns this into Fri Jan 31 2014 19:00:00 GMT-0500 (EST) apparently because it subtracts the 5 hours for my timezone from the entered date. I do not want this. If I enter 2/1/2014 I want that date, regardless of the timezone.
What is the best way to intercept/change/edit this value so that entering 2/1/2014 gives me exactly that date... no conversion for timezone? Should I modify the datepicker code itself (this seems like a bad idea)? Should I change the value myself prior to sending it to the backend by adding in some sort of offset? If so, how do you add time to a value that console displays as Fri Jan 31 2014 19:00:00 GMT-0500 (EST)?
I solved this problem with a custom directive https://gist.github.com/weberste/354a3f0a9ea58e0ea0de
It's for Angular Bootstrap datepicker but I guess it should work for AngularStrap datepicker as well since it only depends on the corresponding ngModel rather than the datepicker itself.
Essentially, I'm reformatting the value whenever a date is selected on the datepicker (this value, a yyyy-mm-dd formatted string, will be stored on the model) and whenever the model is accessed to populate the view, I need to wrap it in a Date object again so datepicker handles it properly.
In other words, it is exactly doing the interceptions that you ask for in your question.
I know this thread is kind of old but since there is no accepted solution I thought I'd offer what finally worked for me after lots of messing around:
The issue in my case was that the datepicker was using the wrong timezone so when I would try to edit an event, the wrong date would display even though the correct date was stored in the db. The following fixed it for me:
var evDate = new Date(data.eventDate); //data.eventDate is the date string
evDate.setMinutes(evDate.getMinutes() + evDate.getTimezoneOffset());
$scope.eventInfo.eventDate = evDate;
I found this solution here: https://github.com/angular-ui/bootstrap/issues/2628
If you don't mind loading another resource, then I would recommend using MomentJS as it takes the pain out of dates in JavaScript. You can do something like `moment(datepicker value).local()' to get the date without the timezone offset.
http://momentjs.com/docs/#/manipulating/local/
I have found a way. convert that date to string first. here is the code.
var SelectDate = $scope.GetFormattedDate(Date.parse($("#Date").datepicker("getDate")));
$scope.GetFormattedDate = function (CalDate) {
var re = /-?\d+/;
var WDate = CalDate.toString();
var m = re.exec(WDate);
var lastDate = new Date(parseInt(m[0]));
var mm = lastDate.getMonth() + 1;
var dd = lastDate.getDate();
var yyyy = lastDate.getFullYear();
var formattedDate = mm + '/' + dd + '/' + yyyy;
return formattedDate;
}
Now Pass SelectDate to your controller. bingo problem has been resolved :)
Guys if you are experiencing this problem is because probably,you are reactivily ,puting data in boostrap datepicker.This solution worked for me.
First you check if date is null
dateBegin===null?dateBegin=null:dateBegin = new Date(formatDate(dateBegin,'yyyy-MM-dd','en'));
**if is null you set its value to null which you are going putt in your Form Group and in Form Group Control
this.ugovorForm = new FormGroup({'dateBegin':new FormControl(dateBegin)})
else you will set Date and format it using formatDate property , provided by Angular.I was struguling with this problem for along time.And its because I'm loading dates from Database.Good Luck;