Javascript - Get a date from any kind of string - javascript

I would like to know if there is a way (I hope there is one, or I am in trouble :p), to find a date in any kind of string.
Here are some examples to make you understand what I am looking for :
var string1 = 'blabla-test-20140215.dat'
I would need to have access to '20140215'
var string2 = 'blabla_Test.20141212'
I would need to have access to '20141212'
Well, I would like to be able to find a date in a format yyyymmdd in a string, no matter the string.
Thank you if you have any clue, I haven't found anything on internet yet.
EDIT :
There can be other numbers in the string, but always less than 8.
for instance :
var string3 = 'blabla-2526-20141212'
The date I am looking for is always separated from other numbers. I can't have :
var string4 = 'blabla-252620141212'
I just want to find the numbers representing the date (in a format yyyymmdd, for instance in string3 I want 20141212, which refers to : 12/12/2014)

I'm note sure, but if your string contains digits only for date, you can use RexEx ?
may be like this :
var regex = /[0-9]+/;
var string1 ="blabla-test-20140215.dat";
var found = regex.exec(string1);
alert('Found: ' + found);

It is simple enough to extract a sequence of eight numbers from a string with regex, just use something like the following:
var dateString = (/\d{8}/.exec(string) || [])[0];
This will find the first eight-character-long string of numbers in a given string. If no such sequence exists, it will be undefined. You can then use it to create a Date object if necessary.

You should use Regex for this.
re = /(\d{4})(\d{2})(\d{2})/g //create a regex that matches: 4 digits followed by 2 digits followed by 2 digits
///// yyyy mm dd
result = re.exec(string1) //execute the regex
// now, access the different capture groups according to their indexes
console.log(result[1]) // yyyy
console.log(result[2]) // mm
console.log(result[3]) // dd

This solution will check for valid dates (not just any 8 arbitrary numbers):
function hasDate(input) {
var matches = input.match(/(\d{4})(\d{2})(\d{2})/),
indexOf = function (elem, arr) {
var len = arr.length, i;
for (i = 0; i < len; i++) {
if (arr[i] === elem) {
return i;
}
}
return -1;
},
months31 = [1, 3, 5, 7, 8, 10, 12],
idx, isLeapYear,
year, month, day;
if (matches) {
year = parseInt(matches[1]);
month = parseInt(matches[2]);
day = parseInt(matches[3]);
//Check invalid dates from the start
if (month < 1 || month > 12) {
return false;
}
else if (day < 1 || day > 31) {
return false;
}
idx = indexOf(month, months31);
isLeapYear = ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
//Month has 31 days, we are good
if (idx >= 0) {
return true;
}
//Feb - check for leap year
else if (month === 2 && (day <= 28 || (isLeapYear && day <= 29))) {
return true;
}
//All other months
else if (month !== 2 && day <= 30) {
return true;
}
}
//If we got this far, its a bad date
return false;
}
//return true
hasDate('blabla_Test.20141212');
//return false
hasDate('blabla_Test.20140229');
//return true
hasDate('blah_20140228');
//return true
hasDate('blah_20000229');

Related

Why is this if statement not affected by my input?

I want to build an algorithm who convert AM/PM to the 24hours format. It's not finished, but the code I have so far is behaving strangely.
When I give the input "25:05:45PM", it should enter the first branch of the first if statement, but should not enter the second if statement. I've checked the condition, and it's definitely false. My brain is melting.
Here is the code :
function conversionTime(s) {
if (s.includes('PM')) {
let temp = s.slice(0, 8).split(':');
if (temp[0] >= 01 && temp[0] <= 12); {
temp[0] = Number(temp[0]) + 12;
return temp.join(':')
}
} else if (s.includes('AM')) {
let temp2 = s.slice(0, 8).split(':');
return temp2
}
}
console.log(conversionTime("25:05:45PM"))
Gotcha.
if (temp[0] >= 01 && temp[0] <= 12);
This semicolon is the culprit! It's saying "the if statement is over, no need to do anything", so your code is being interpreted like:
if (temp[0] >= 01 && temp[0] <= 12);
{
temp[0] = Number(temp[0]) + 12;
return temp.join(':');
}
The code in the block will always run. This feature exists so you can make full use of let's scoping:
let x = "outside";
console.log(x);
{
let x = "inside";
console.log(x);
}
console.log(x);
Well, really it exists because that's how C works – it predates the let statement – but that's what it's useful for these days.
I Will do that this way..
function conversionTime(timeAPM)
{
let [h,m,s,apm] = timeAPM.match(/(\d+)|(\w+)/g);
if (timeAPM !== `${h}:${m}:${s}${apm}`
|| isNaN(h) || isNaN(m) || isNaN(s)
|| (apm !== 'AM' && apm !== 'PM')
) return undefined;
if (apm === 'PM' && h <= 12 ) h = +h +12;
return `${h}:${m}:${s}`;
}
console.log(conversionTime("25:05:45PM"))
I don't know if it's intentional or not on his part, but the PO's code also returns undefined. Apart from his attachment to the AM/PM labels, the OP gives no explanation on the validity of the entry.
So this second version is content to check that there are indeed 3 numerical values, separate by :, and directly followed by AM or PM.
the number of digits of these numbers is unknown : His test uses an hour = 25 while it is an AM/PM presentation. So why not seconds or minutes expressed with 40 digits...
I'm not claiming my regex is the best in the universe.
I could also have added in my code:
console.log(conversionTime("10:31:25BAM")) // -> undefined
console.log(conversionTime("1:3:5abc")) // -> undefined
console.log(conversionTime("1:3zzzz")) // -> undefined
console.log(conversionTime("a:10:15PM")) // -> undefined
console.log(conversionTime("285::4875PM")) // -> undefined
// or more absurd:
console.log(conversionTime("285:1505:4875PM")) // -> 285:1505:4875

How do I validate a date in this format (yyyy-mm-dd) using jquery?

I am attempting to validate a date in this format: (yyyy-mm-dd). I found this solution but it is in the wrong format for what I need, as in: (mm/dd/yyyy).
Here is the link to that solution: http://jsfiddle.net/ravi1989/EywSP/848/
My code is below:
function isDate(txtDate)
{
var currVal = txtDate;
if(currVal == '')
return false;
var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex
var dtArray = currVal.match(rxDatePattern); // is format OK?
if (dtArray == null)
return false;
//Checks for mm/dd/yyyy format.
dtMonth = dtArray[1];
dtDay= dtArray[3];
dtYear = dtArray[5];
if (dtMonth < 1 || dtMonth > 12)
return false;
else if (dtDay < 1 || dtDay> 31)
return false;
else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
return false;
else if (dtMonth == 2)
{
var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
if (dtDay> 29 || (dtDay ==29 && !isleap))
return false;
}
return true;
}
What regex pattern can I use for this that will account for invalid dates and leap years?
I expanded just slightly on the isValidDate function Thorbin posted above (using a regex). We use a regex to check the format (to prevent us from getting another format which would be valid for Date). After this loose check we then actually run it through the Date constructor and return true or false if it is valid within this format. If it is not a valid date we will get false from this function.
function isValidDate(dateString) {
var regEx = /^\d{4}-\d{2}-\d{2}$/;
if(!dateString.match(regEx)) return false; // Invalid format
var d = new Date(dateString);
var dNum = d.getTime();
if(!dNum && dNum !== 0) return false; // NaN value, Invalid date
return d.toISOString().slice(0,10) === dateString;
}
/* Example Uses */
console.log(isValidDate("0000-00-00")); // false
console.log(isValidDate("2015-01-40")); // false
console.log(isValidDate("2016-11-25")); // true
console.log(isValidDate("1970-01-01")); // true = epoch
console.log(isValidDate("2016-02-29")); // true = leap day
console.log(isValidDate("2013-02-29")); // false = not leap day
You could also just use regular expressions to accomplish a slightly simpler job if this is enough for you (e.g. as seen in [1]).
They are build in into javascript so you can use them without any libraries.
function isValidDate(dateString) {
var regEx = /^\d{4}-\d{2}-\d{2}$/;
return dateString.match(regEx) != null;
}
would be a function to check if the given string is four numbers - two numbers - two numbers (almost yyyy-mm-dd). But you can do even more with more complex expressions, e.g. check [2].
isValidDate("23-03-2012") // false
isValidDate("1987-12-24") // true
isValidDate("22-03-1981") // false
isValidDate("0000-00-00") // true
[1]
Javascript - Regex to validate date format
[2] http://www.regular-expressions.info/dates.html
Since jQuery is tagged, here's an easy / user-friendly way to validate a field that must be a date (you will need the jQuery validation plugin):
html
<form id="frm">
<input id="date_creation" name="date_creation" type="text" />
</form>
jQuery
$('#frm').validate({
rules: {
date_creation: {
required: true,
date: true
}
}
});
DEMO + Example
UPDATE: After some digging, I found no evidence of a ready-to-go parameter to set a specific date format.
However, you can plug in the regex of your choice in a custom rule :)
$.validator.addMethod(
"myDateFormat",
function(value, element) {
// yyyy-mm-dd
var re = /^\d{4}-\d{1,2}-\d{1,2}$/;
// valid if optional and empty OR if it passes the regex test
return (this.optional(element) && value=="") || re.test(value);
}
);
$('#frm').validate({
rules: {
date_creation: {
// not optional
required: true,
// valid date
date: true
}
}
});
This new rule would imply an update on your markup:
<input id="date_creation" name="date_creation" type="text" class="myDateFormat" />
Here's the JavaScript rejex for YYYY-MM-DD format
/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/
try this Here is working Demo:
$(function() {
$('#btnSubmit').bind('click', function(){
var txtVal = $('#txtDate').val();
if(isDate(txtVal))
alert('Valid Date');
else
alert('Invalid Date');
});
function isDate(txtDate)
{
var currVal = txtDate;
if(currVal == '')
return false;
var rxDatePattern = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/; //Declare Regex
var dtArray = currVal.match(rxDatePattern); // is format OK?
if (dtArray == null)
return false;
//Checks for mm/dd/yyyy format.
dtMonth = dtArray[3];
dtDay= dtArray[5];
dtYear = dtArray[1];
if (dtMonth < 1 || dtMonth > 12)
return false;
else if (dtDay < 1 || dtDay> 31)
return false;
else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
return false;
else if (dtMonth == 2)
{
var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
if (dtDay> 29 || (dtDay ==29 && !isleap))
return false;
}
return true;
}
});
changed regex is:
var rxDatePattern = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/; //Declare Regex
I recommend to use the
Using jquery validation plugin and jquery ui date picker
jQuery.validator.addMethod("customDateValidator", function(value, element) {
// dd-mm-yyyy
var re = /^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$/ ;
if (! re.test(value) ) return false
// parseDate throws exception if the value is invalid
try{jQuery.datepicker.parseDate( 'dd-mm-yy', value);return true ;}
catch(e){return false;}
},
"Please enter a valid date format dd-mm-yyyy"
);
this.ui.form.validate({
debug: true,
rules : {
title : { required : true, minlength: 4 },
date : { required: true, customDateValidator: true }
}
}) ;
Using Jquery and date picker just create a function with
// dd-mm-yyyy
var re = /^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$/ ;
if (! re.test(value) ) return false
// parseDate throws exception if the value is invalid
try{jQuery.datepicker.parseDate( 'dd-mm-yy', value);return true ;}
catch(e){return false;}
You might use only the regular expression for validation
// dd-mm-yyyy
var re = /^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$/ ;
return re.test(value)
Of course the date format should be of your region
moment(dateString, 'YYYY-MM-DD', true).isValid() ||
moment(dateString, 'YYYY-M-DD', true).isValid() ||
moment(dateString, 'YYYY-MM-D', true).isValid();
Just use Date constructor to compare with string input:
function isDate(str) {
return 'string' === typeof str && (dt = new Date(str)) && !isNaN(dt) && str === dt.toISOString().substr(0, 10);
}
console.log(isDate("2018-08-09"));
console.log(isDate("2008-23-03"));
console.log(isDate("0000-00-00"));
console.log(isDate("2002-02-29"));
console.log(isDate("2004-02-29"));
Edited: Responding to one of the comments
Hi, it does not work on IE8 do you have a solution for – Mehdi Jalal
function pad(n) {
return (10 > n ? ('0' + n) : (n));
}
function isDate(str) {
if ('string' !== typeof str || !/\d{4}\-\d{2}\-\d{2}/.test(str)) {
return false;
}
var dt = new Date(str.replace(/\-/g, '/'));
return dt && !isNaN(dt) && 0 === str.localeCompare([dt.getFullYear(), pad(1 + dt.getMonth()), pad(dt.getDate())].join('-'));
}
console.log(isDate("2018-08-09"));
console.log(isDate("2008-23-03"));
console.log(isDate("0000-00-00"));
console.log(isDate("2002-02-29"));
console.log(isDate("2004-02-29"));
Rearrange the regex to:
/^(\d{4})([\/-])(\d{1,2})\2(\d{1,2})$/
I have done a little more than just rearrange the terms, I've also made it so that it won't accept "broken" dates like yyyy-mm/dd.
After that, you need to adjust your dtMonth etc. variables like so:
dtYear = dtArray[1];
dtMonth = dtArray[3];
dtDay = dtArray[4];
After that, the code should work just fine.
Working Demo fiddle here Demo
Changed your validation function to this
function isDate(txtDate)
{
return txtDate.match(/^d\d?\/\d\d?\/\d\d\d\d$/);
}
You can use this one it's for YYYY-MM-DD. It checks if it's a valid date and that the value is not NULL. It returns TRUE if everythings check out to be correct or FALSE if anything is invalid. It doesn't get easier then this!
function validateDate(date) {
var matches = /^(\d{4})[-\/](\d{2})[-\/](\d{2})$/.exec(date);
if (matches == null) return false;
var d = matches[3];
var m = matches[2] - 1;
var y = matches[1] ;
var composedDate = new Date(y, m, d);
return composedDate.getDate() == d &&
composedDate.getMonth() == m &&
composedDate.getFullYear() == y;
}
Be aware that months need to be subtracted like this: var m = matches[2] - 1; else the new Date() instance won't be properly made.

Check whether white spaces exist without using trim

I have following code that checks whether date is valid. http://jsfiddle.net/uzSU6/36/
If there is blank spaces in date part, month part or year part the date should be considered invalid. For this, currently I am checking the length of string before and after trim operation. It works fine. However is there a better method to check for white spaces? (For example, using === operator)
function isValidDate(s)
{
var bits = s.split('/');
//Javascript month starts at zero
var d = new Date(bits[2], bits[0] - 1, bits[1]);
if ( isNaN( Number(bits[2]) ) )
{
//Year is not valid number
return false;
}
if ( Number(bits[2]) < 1 )
{
//Year should be greater than zero
return false;
}
//If there is unwanted blank space, return false
if ( ( bits[2].length != $.trim(bits[2]).length ) ||
( bits[1].length != $.trim(bits[1]).length ) ||
( bits[0].length != $.trim(bits[0]).length ) )
{
return false;
}
//1. Check whether the year is a Number
//2. Check whether the date parts are eqaul to original date components
//3. Check whether d is valid
return d && ( (d.getMonth() + 1) == bits[0]) && (d.getDate() == Number(bits[1]) );
}
You can use indexOf() function if there is space in the date string.
if(s.indexOf(' ') != -1)
{
//space exists
}
you can test for any whitespace like
if( /\s/g.test(s) )
it will test any whitespace.
You may want to consider using a regexp to test your string's validity:
function isValidDate(s) {
var re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
var mdy = s.match(re);
if (!mdy) {
return false; // string syntax invalid;
}
var d = new Date(mdy[3], mdy[1] - 1, mdy[2]);
return (d.getFullYear() == mdy[3]) &&
(d.getMonth() == mdy[1] - 1) &&
(d.getDate() == mdy[2]);
}
The regex does all this in one go:
checks that there are three fields separated by slashes
requires that the fields be numbers
allows day and month to be 1 or 2 digits, but requires 4 for the year
ensures that nothing else in the string is legal
See http://jsfiddle.net/alnitak/pk4wU/

Format currency using javascript

a script returns either a number like 0.0580 so in x.xxxx format or a (x) for X units left.
I want to format the number 0.0580 and return 5.8 cent or return x units left.
Any ideas how to do that in javascript? Especially how do I format the x.xxxx?
In case the first x is not 0 I want to return e.g. 1.75$.
MS has written a nice plugin for jquery. it's especially useful if you're localizing. Give it a go:
http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx
I'm not sure if this can be used outside of jquery...
I may be spoiling you here, but whatever. Here's a function that I found somewhere at some point and have been recycling since. I haven't actually bothered to look much into it to figure out what it does exactly, but it has been rather useful:
function FormatMoneyAmount(starting_string, ending_string) {
//check validity of input (true = invalid, false = valid)
var valid_exp = new RegExp ('[^0-9,.$]', 'gi');
input_invalid = (typeof(ending_string) == 'undefined' && valid_exp.test(starting_string));
//check if more than 2 digits follow decimal or no decimal
decimal_invalid = typeof(ending_string) == 'undefined' && (starting_string.indexOf('.') > -1) && ((starting_string.length - starting_string.indexOf('.')) > 3);
if (input_invalid || decimal_invalid) {
ending_string = starting_string;
} else {
//remove commas, dollar signs
var replace_exp = new RegExp ('[,$]', 'gi');
starting_string = starting_string.replace(replace_exp, '');
//remove decimal if ending string not set, save for adding on later
var decimal_substring = '';
if (typeof(ending_string) == 'undefined' && starting_string.indexOf('.') > -1) {
decimal_substring = starting_string.substring(starting_string.indexOf('.'), starting_string.length);
remaining_string = starting_string.substring(0,starting_string.indexOf('.'));
} else {
remaining_string = starting_string;
}
//if string is already 3 characters or less, do nothing
if (remaining_string.length > 3) {
//separate last 3 characters of string from rest of string
var final_three = remaining_string.substring(remaining_string.length - 3, remaining_string.length);
remaining_string = remaining_string.substring(0, remaining_string.length - 3);
//if not first group of 3, add new group before old group with comma, else set to new group
ending_string = (typeof(ending_string) == 'undefined') ? final_three + ((typeof(decimal_substring) == 'undefined') ? '' : decimal_substring) : final_three + ',' + ending_string;
//call function again if more than 3 digits remaining to process, else add to end string
if (remaining_string.length > 3) {
ending_string = FormatMoneyAmount(remaining_string, ending_string);
} else {
ending_string = remaining_string + ',' + ending_string;
}
} else {
ending_string = (typeof(ending_string) == 'undefined') ? remaining_string : remaining_string + ',' + ending_string + ((typeof(decimal_substring) == 'undefined') ? '' : decimal_substring);
}
}
return ending_string;
}
The first thing to do is check the format of the string, since you will have two code paths depending on the result:
if (typeof num = "string" && num.slice(0,1) == "(" && num.slice(-1) == ")") {
// String is in the format (x), so we just need to return that number
return num.slice(1,-1) + " units left";
}
The next part is to check if the number is less than 1, indicating that it is cents and not whole dollars. If it is less than 1, multiplying it by 100 will give you the number of cents you're after:
if (+num < 1)
// 0.0580 * 100 = 5.8
return (num * 100) + " cents";
else
return +num + "$";

Is there a more compact way of checking if a number is within a range?

I want to be able to test whether a value is within a number range. This is my current code:
if ((year < 2099) && (year > 1990)){
return 'good stuff';
}
Is there a simpler way to do this? For example, is there something like this?
if (1990 < year < 2099){
return 'good stuff';
}
In many languages, the second way will be evaluated from left to right incorrectly with regard to what you want.
In C, for instance, 1990 < year will evaluate to 0 or 1, which then becomes 1 < 2099, which is always true, of course.
Javascript is a quite similar to C: 1990 < year returns true or false, and those boolean expressions seem to numerically compare equal to 0 and 1 respectively.
But in C#, it won't even compile, giving you the error:
error CS0019: Operator '<' cannot be applied to operands of type 'bool' and 'int'
You get a similar error from Ruby, while Haskell tells you that you cannot use < twice in the same infix expression.
Off the top of my head, Python is the only language that I'm sure handles the "between" setup that way:
>>> year = 5
>>> 1990 < year < 2099
False
>>> year = 2000
>>> 1990 < year < 2099
True
The bottom line is that the first way (x < y && y < z) is always your safest bet.
You could make your own method:
// jquery
$(function() {
var myNumber = 100;
try {
if (myNumber.isBetween(50, 150))
alert(myNumber + " is between 50 and 100.");
else
alert(myNumber + " is not between 50 and 100.");
} catch (e) {
alert(e.message());
}
});
// js prototype
if (typeof(Number.prototype.isBetween) === "undefined") {
Number.prototype.isBetween = function(min, max, notBoundaries) {
var between = false;
if (notBoundaries) {
if ((this < max) && (this > min)) between = true;
alert('notBoundaries');
} else {
if ((this <= max) && (this >= min)) between = true;
alert('Boundaries');
}
alert('here');
return between;
}
}
hope this helps.
Max
The fast and simple way to make this is to create a function like this:
function inRange(n, nStart, nEnd)
{
if(n>=nStart && n<=nEnd) return true;
else return false;
}
Then use that as follows:
inRange(500, 200, 1000) => this return true;
Or like this:
inRange(199, 200, 1000) => this return false;
If you don't like the boolean operator, you could always use nested if statements:
if (1990 < year)
{
if( year < 2099)
return 'good stuff';
}
From a similar solution here: http://indisnip.wordpress.com/2010/08/26/quicktip-check-if-a-number-is-between-two-numbers/
$.fn.between = function(a,b){
return (a < b ? this[0] >= a && this[0] <= b : this[0] >= b && this[0] <= a);
}

Categories