This question already has answers here:
How to parse a date in format "YYYYmmdd" in JavaScript?
(6 answers)
Closed 6 years ago.
I am pulling a date of birth as a string from a form on a website im looking after. I need to be able to derive an age from that date string.
My thoughts are converting it into a proper date using a split (its delimited by "%2F") and calculating from that but my syntax isnt that good so im having real trouble.
The code im working with to pull the string I need is;
function() {
var inputField = document.getElementById("date-of-birth-input");
return inputField.value || "";
}
Any help would be appreciated.
If you have the user input the date in a format like 1/1/1990, you could do something like this:
var inputField = document.getElementById("date-of-birth-input"); // `1/1/1990`
var today = new Date().getFullYear();
var birthdate = new Date(inputField).getFullYear();
var age = today - birthdate;
The JavaScript Date constructor is pretty flexible in what it can turn into a date, so you might not need to go through splitting-string hassles. Check out the docs.
Date format varies in different countries. In US the date format is MM-DD-YYYY while in Asian countries the date format is DD-MM-YYYY and in China its 'YYYY-MM-DD'. The right way to go about dealing with dates is using Moment.js
If you have a simple application/website include moment in your <head> tag as follows
<script src="moment.js"></script>
(Hoping you have downloaded moment.js and kept in the same structure as index.html). Otherwise use this if you want to use it directly from the net without downloading.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/locale/af.js"></script>
Then use the following
function() {
/* First-make sure you format moment according to your giving locale.
I'm formating here according to the US date format */
var inputField = moment(document.getElementById("date-of-birth-input")).format("MM-DD-YYYY");
return moment().diff(inputField, 'years');
}
I hope this helps. Thanks.
Related
This question already has answers here:
Format a date string in javascript
(7 answers)
Closed 2 years ago.
How do I format the date I receive from openweather api?
I currently get 2020-10-16 00:00:00 and I want 10-16-2020.
The reason I don't use moment is because I want future dates which come automatically with the 5 day forecast in the api.
You can use JavaScript's Date object.
You might save yourself time by searching a bit more before posting a question, the answer is probably already out there.
Where can I find documentation on formatting a date in JavaScript?
How to format a JavaScript date
Format JavaScript date as yyyy-mm-dd
You could try to:
Make a new date based on the date that comes from OpenWeather api
Convert it to a LocaleDateString using the 'en-US' locale. This will make the month appear before the date.
Then just split the Date String on the '/' and join in on a '-'. This will substitute the '/' with a '-'
const date = new Date("2020-10-16 00:00:00").toLocaleDateString('en-US');
const formatedDate = date.split('/').join('-');
console.log(formatedDate);
You can always use the built in Javascript Date object.
In your case you'd want to. do something like this -
const myDate = new Date('2020-10-16 00:00:00');
const date = myDate.getDate();
const month = myDate.getMonth() + 1;
const year = myDate.getFullYear();
console.log('MM-DD-YYYY', `${month}-${date}-${year}`);
If you want something more convinient and don't want to use moment you can also try some other popular date libraries. You can find some here - https://github.com/you-dont-need/You-Dont-Need-Momentjs
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I'm working on legacy system and in DB the birthday is coming this way '1992-05-18' in json. I am using AngularJS and when applying the data binding of this variable in an input type = "date", of an update form, it is necessary to instantiate a Date object. Like this:
//person.byrthday = '1992-04-26'
var person.birthday = new Date (person.birthday);
// after person.byrthday = '1992-04-25T00:00:00.000Z'
How can I solve this problem through Front End in an elegant way, without "breaking" two way data binding?
I find myself in Brasil UTC -03:00
There are a few ways to solve this problem. A quick and dirty solution could be to leverage moment.js. You can convert the response from the API to a true date format this way.
If you don't want to use an additionally library, you can make a function to parse the date string. You can do the following to parse is to become a correct date:
var dateSplit = person.birthday.split('-');
var mydate = new Date(dateSplit[0], dateSplit[1] - 1, dateSplit[2]);
person.birthday= mydate;
Take note that the month index starts at 0 (aka January=0). Hopefully this helps.
I am trying to collect a date from text boxes and then do a comparison on them. I am entering the dates in dd-MM-yyyy format, however when the comparison is running its running is in a MM-dd-yyyy format.
my fiddle is here
in my live application I am using a bootstrap datepicker to enter the date so the date entered will always be the correct format.
Ive looked here at W3 Schools and I have also tried looking at
var dt1 = d1.split(/\-|\s/)
var dt2 = d2.split(/\-|\s/)
dat1 = new Date(dt1);
dat2 = new Date(dt2);
dat1.format("dd-MM-yyyy")
but this also fails.
Any and all help very much appreciated.
thanks
Use the getTime() method if you want to make comparison on dates :
if(dat1.getTime() > dat2.getTime()) { ... }
On your example : https://jsfiddle.net/3ut8a7zj/1/
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Compare 2 dates with JavaScript
Hi,
I'm working on form validation for my application and i want to know where i would be to start looking at to find date comparison eg a start date and an end date. The dates inputted in to my application are in the form: DD/MM/YYYY.
Thanks in Advance,
Dean
If you are using the Javascript Date object, you can simply do:
var startDate = new Date();
var endDate = getEndDate();
if (endDate < startDate)
alert("Houston, we've got a problem!");
EDIT: Changed naming a bit just to stick to camelCase convention, even though I despise it.
this function lets you convert dates to timestamps with wich you could work:
http://caioariede.com/arquivos/strtotime.js
First, you'll want to parse the text into Date objects, then use the language's built-in date comparison. For example:
var dateStr = document.getElementById('foo').value;
var date = Date.parse(dateStr);
var dateStr2 = document.getElementById('foo2').value;
var date2 = Date.parse(dateStr2);
if (date < date2) {
// ...
}
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
How can I convert datetime microformat to local time in javascript?
Im writing up an ajax application where i have to interpret this date "2009-09-16T11:10:00" and output another string to something more readable.
That's the ISO 8601 date format. There's an example here. If that doesn't suit your needs then a quick google search should help.
No, there isn't a built-in function for doing that. You'd have to parse it yourself. Maybe something like this:
var s = "2009-09-16T11:10:00";
var tokens = s.split(/[\-T:]/);
var date = new Date(tokens[0], tokens[1] - 1, tokens[2],
tokens[3], tokens[4], tokens[5], 0);
Then access the date string with:
alert(date.toString());
Try this js library:
http://www.datejs.com
Pretty good and recognizes different date formats. You can also test your date right on the front page.