Unable to set Date Format with Javascript - javascript

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/

Related

How to get previous month and expected format from month picker using moment JS

I am using jquery.ui.monthpicker library. For month picker I am getting date like 07/2017. From this date string I need to calculate previous month and formatted like 1707 using moment js library.
any help would be appreciated.
This code may solve your problem.
moment("07/2017", "MM/YYYY").subtract(1, 'months').format('YYMM');
DEMO at https://jsfiddle.net/nffswx75/
var dt = "07/2017";
alert(moment(dt,"MM/YYYYY").format('YYMM'));
alert(moment(dt,"MM/YYYYY").add(-1, 'months').format('YYMM'));
alert(moment(dt,"MM/YYYYY").subtract(1, 'months').format('YYMM'));
You can let moment create a date object from a string by telling it what format your date is in.
let dateString: string = "07/2017";
var date = moment(dateString, "MM/YYYY").subtract(1, 'month').format("YYMM");

Custom Javascript Variable in GTM to convert date (string) to date [duplicate]

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.

moment js cannot change date string to new format

I have the following HTML:
<strong id="ut-open-date">27/06/2014</strong>
and I want to read the text/innerhtml and convert its format to "YYYY-MM-DD" so I can insert it into MySQL table. I am using the moment.js library and my code is below:
var CreateDate = moment(jQuery('#ut-open-date').html()).format("DD/MM/YYYY");
CreateDate = moment(CreateDate).format("YYYY-MM-DD");
But the code changes 27/06/2014 to 2016-06-03 and I cannot work out why.
I also tried this code with the same result.
var CreateDate = moment(jQuery('#ut-open-date').html()).format("YYYY-MM-DD");
Any help is appreciated.
If we break down your code step by step you can see where it is going wrong:
var CreateDate = moment(jQuery('#ut-open-date').html())
This part uses the default constructor to try to parse the date, this is unreliable at best and has been deprecated. So moment is trying to guess what the date format is here.
.format("DD/MM/YYYY");
This is taking what ever was read in step 1 and trying to turn it into a string with the format of DD/MM/YYYY
CreateDate = moment(CreateDate)
Now you are parsing again without specifying the format so moment is doing it's best to guess
.format("YYYY-MM-DD");
Now you have told it to turn whatever it guessed the date to be into a string with the format YYYY-MM-DD
Do this instead:
var CreateDate = moment(jQuery('#ut-open-date').html(), 'DD/MM/YYYY').format('YYYY-MM-DD');
The moment(dateString) form is deprecated you should use the form moment(dateString, expectedFormat) instead.
See moment documentation here: http://momentjs.com/docs/#/parsing/string-format/
Thanks to athms for link
The problem is you need to tell moment.js what format for date string you want parse by specifying second parameter. See all the supported format. If your format is not listed (See the Open Issue for NON-ISO strings), you need to specify the date format parameter.
moment( jQuery('#ut-open-date').html(), "DD/MM/YYYY" )
DEMO
I solved it by using split as follows:
var CreateDate = jQuery('#ut-open-date').text();
var DateArray = CreateDate.split('/');
CreateDate = DateArray[2] + '-' + DateArray[1] + '-' + DateArray[0];

Parse month text using parseDate utility method

I'm having trouble parsing a month year string into a date when using the jquery ui 's utility method $.datepicker.parseDate(format, string, settings). ( https://api.jqueryui.com/datepicker/ )
When I use it like
var dt = $.datepicker.parseDate('MM yy', 'January 2011');
I expect it to return a date which equals new Date(2011,00,1), but instead I get an invalid date. Does jqueryui's parseDate support full date string parsing? I can't really find any good docs on this. I expected it to support the full month parsing because the datepicker is able to display it in full month format. I've set up a fiddle* to demonstrate my problem. The core code from the fiddle is below.
*http://jsfiddle.net/mouseoctopus/fvrpG/
var text = $('#dateText').val();
var format = $('#dateFormat').val();
$('#instructions').text('Parse the date [' + text + '] using the format [' + format +']');
try{
var dt = $.datepicker.parseDate(format,text);
$('#result').text(dt);
}catch(exception){ }
So my question is.. it possible to parse a full month text using the parseDate?
I can't speak for jQuery parseDate, but you can use a native JavaScript Date.parse with the full month. The JavaScript function requires a full date though, so you'll need to add 1 in front of the date. It returns a timestamp which you can use it to create a new date.
You can find complete documentation at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
Example
alert(new Date(Date.parse('1 January 2011')));

Validating a UK date using Javascript/jQuery

I have two jQuery datepickers that once changed, will trigger some ajax to grab all information between the two dates.
I want to run some code to check that the first date is smaller than the second, by converting to a date using this code:
function FormatUkDate(dateStr) {
dateStr = dateStr.split("/");
return new Date(dateStr[2], dateStr[1] - 1, dateStr[0]);
}
This works great, but the problem is even if I enter a date of '50/08/2011' it still validates and converts that to a Javascript date, I believe by adding the additional number of days to the start date.
Is there a way to properly validate this please?
Thanks!
you can validate using a jquery masked plugin,you can check it http://digitalbush.com/projects/masked-input-plugin/
For working with dates you can try to use datejs library. It has many features including validating dates. To solve your problem you can use Date.validateDay method. Also you can use datejs to compare dates.
hm... I guess a plugin would be a better solution, but for what it's worth:
function FormatUkDate(dateStr) {
dateStr = dateStr.split("/");
var newDate = new Date(dateStr[2], dateStr[1] - 1, dateStr[0]);
return newDate.getDate() == Number(dateStr[0]) && newDate.getMonth() == Number(dateStr[1]) - 1? newDate : null;
}
returns null if the date carries over to the next month.
Edit
New code :P

Categories

Resources