I am getting data through Django api into my web application and the date return is "2015-09-10T00:00:00Z", is there a javscript function that can turn this into format like 2015-09-10
var date = new Date("2015-09-10T00:00:00Z");
var result = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
This is cumbersome and error prone, also it removes the leading 0 for the month and day. Take a look at this js library: mement.js, it should provide whatever you need to manipulate the time.
It's very light weight and easy to use.
Related
So I'm making a reservations form, and I'll store all the data including the date in the database, but I don't want the certain dates to show if certain time has pass today, (example 12:00 CET), or if the date is unavaliable, either due to date from database-tbl_unavaliable or due to reaching max amnout of reservations for the day (10 for example).
So far after a few hours of searching I've found only this on SO, I don't know if the thing i want is doable or not with php since it's kind of the only thing I currently somewhat know and I'm still currently learning it. I do not know anything of JavaScript. But is there a kind of way to maybe get a $variable with php and make that a condition in JavaScript or something similar?
<script>
function getISODate(){
var d = new Date();
return d.getFullYear() + '-' +
('0' + (d.getMonth()+1)).slice(-2) + '-' +
('0' + d.getDate()).slice(-2);
}
window.onload = function() {
document.getElementById('minToday').setAttribute('min',getISODate());
}
</script>
<p>Select date(must be on or after today)<input type="date" id="minToday"></p>
I am using javascript date and I am trying to get it in my own format(YYYY-MM-DD). However, I could manage the format but the problem here is I am getting a day ahead. For eg: if the day is 3(today), here with this code I am getting 4.
let currentDate = new Date(currentForm['dobs']);
let middleDate = currentDate.toLocaleDateString().split('-');
let finalDate = middleDate[0] + '-' + middleDate[1] + '-' + (middleDate[2] - 1);
console.log(finalDate);
Here, in currentDate I am getting date as: 2051-06-30T18:15:00.000Z
But, in finalDate, I am getting 2049-6-31
Ignoring the syntax, why am I getting a day ahead ? How to achieve a correct date ??
I used to have this question few years ago and the reason is really simple. It's because of the 'Z' character at the end of the date value, because for the .toLocaleDateString() function(given that you haven't specified parameters, here is more info) implies this value lies in the UTC+0 zone and for some kind of reason that is always throwing you one day a head.
Solution:
To solve this you have many options, but maybe the simplest one is to format the date by yourself like this:
function lz(n){return n<10 ? '0'+n : n}
var d = new Date('2019-01-01T00:00:00Z');
var formatted = d.getUTCFullYear()+'-'
+ lz(d.getUTCMonth()+1)+'-'
+ lz(d.getUTCDate())+' '
+ lz(d.getUTCHours())+':'
+ lz(d.getUTCMinutes())+':'
+ lz(d.getUTCSeconds());
Or you can use third-party libraries like moment.js which, by the way, is pretty popular. Here is a link for more info.
Edit:
My apologies I was omitting one part of my code since I took it directly from one of my projects. Please follow the above code or test it in this fiddle.
As per #Alfredo Zamudio answer, I tried to remove that 'Z' attached at the end of my date value and after that everything ok.
let currentDate = new Date(currentForm['dobs']);
let newDate = currentDate.toISOString().replace('Z', '');
After these lines of code my issue was solved and then I used simple technique to format date.
let formattedDate = newDate.getFullYear() + '-' + newDate.getMonth() + '-' + newDate.getDate();
Thanks to all !
I need to convert my date to mm-dd-yyyy format. So I used a method like this:
var dt=new Date(2016-06-21);
var ddte='';
ddte=(("0" + (dt.getMonth() + 1)).slice(-2))+"-"+(("0" + dt.getDate()).slice(-2))+"-"+dt.getFullYear();
It works fine in my local timezone (GMT+05:30). But when I change my timezone to GMT -5:00, it gives the wrong result: 06-20-2016. The result I want is 06-21-2016.
Can anyone please explain the problem?
How can I get the correct result?
Is it a bug?
Your date passed to Date() constructor will be treated as UTC time zone. Getting the time with Date.getMonth() will get your local time zone. You're probably looking for Date.getUTCMonth().
var dt=new Date("2016-06-21");
var ddte='';
ddte=(("0" + (dt.getUTCMonth() + 1)).slice(-2))+"-"+(("0" + dt.getUTCDate()).slice(-2))+"-"+dt.getUTCFullYear();
console.log(ddte);
Though in this case I see no use for using Date at all; this should suffice:
var parsedDate = "2016-06-21".replace(/(\d{4})-(\d{2})-(\d{2})/, "$2-$3-$1");
console.log(parsedDate);
It isn't a bug. It's just how time zones work (it isn't the same calendar day everywhere in the world at the same time).
If you don't actually want advanced date features (it seems you only want some good old string manipulation) my tip is to just not use Date in the first place.
var parts = "2016-06-21".split("-");
var mdy = parts[1] + "-" + parts[2] + "-" + parts[0];
Add some error checking and you're done.
I want send a datetime in JSON with string fromat from asp.net web service and I want javascript parse it like datetime and not like string.
So I ask is there a special format that I must use it to convert datetime to string and parse it in javascript as datetime?
In fact , I don't want touch javascript. I want javascript read the string and considerate it as DateTime.
Make sure your date strings conform rfc2822 and use javascript Date.parse method.
Alternatively, send your dates over as integer milliseconds and use Date constructors on the javascript side.
I used DateJS for a few projects using ASP.NET. The format will probably depend on your locale. I would looks at the data.js examples to make it work the way it would fit your specification and locale.
This is a piece of code I use to create a date from a .net JSON serialized DateTime object.
function formatJSONDate(jsonDate) {
var d = new Date((jsonDate.slice(6, -2)*1));
var day = d.getDate() * 1;
var month = (d.getMonth() * 1) +1;
var s = (day < 10 ? "0" : "") + day + "-" + (month < 10 ? "0" : "") + month + "-" + d.getFullYear();
return s;
}
I'm using calendar from Yahoo UI as follows:
calDate = (calDate.getMonth() + 1) +
'/' + calDate.getDate() + '/' +
calDate.getFullYear();
This displays a MMDDYYYY format.
I want to change this format to YYYY-MM-DD so that I can insert it into a MySQL database.
I changed the code to:
calDate = calDate.getFullYear() + '-'
+ (calDate.getMonth() + 1) + '-' + calDate.getDate();
It worked but the problem is now that when I want to change the selected date the calender doesn't display the date and instead shows NAN.
It's typically best to pass the date to your back end as a unix timestamp and convert it in the database itself. In the code where you construct your POST data for the server, you'd pass something like this:
var t = calDate.getTime()/1000;
Note the divide by 1000. This is required because javascript timestamps are in milliseconds, while MySQL requires seconds.
In your SQL statement, you'll pass the timestamp as is, and use the FROM_UNIXTIME function to convert it to your required format:
INSERT INTO ... VALUES ( FROM_UNIXTIME($t), ...)
Naturally there will be some code in between that converts t from javascript into $t in your back end script, and then passes that on to the SQL.
Now, if you really need to format dates on the front end, you can make use of the handy YAHOO.util.Date utility. Just do something like this:
alert(YAHOO.util.Date.format(calDate, {format: "%Y-%m-%d" }));
Much easier than calling getFullYear, getMonth, and getDate
bluesmoon has also written two excellent articles on YUIBlog on the subject of date formatting:
http://yuiblog.com/blog/2009/02/11/date-formatting-pt1-2/
http://yuiblog.com/blog/2009/02/25/date-formatting-pt2/
-Eric
You should change the format of the date just before it is inserted on the server side, let the client side have the format that it wants, and change the date around later.
cal1.cfg.setProperty("DATE_FIELD_DELIMITER", "-");
cal1.cfg.setProperty("MDY_YEAR_POSITION", 1);
cal1.cfg.setProperty("MDY_MONTH_POSITION", 2);
cal1.cfg.setProperty("MDY_DAY_POSITION", 3);