How to convert the Date into words? - javascript

Can anyone help me regarding the conversion of Date into words - e.g: '12/04/2011' convert to 'one twelve zero four two zero one one' ...?
Can we do this using javascript?
Do we have any plugin in jquery?

I would use an array:
var numbers = ['zero','one','two'...]
Then loop through the string containing the date (dateVar) using the array as a reference for the names.
for (i=1;i<dateVar.length;i++){
document.write( numbers[parseInt(dateVar.charAt(i))]+' ');
}
Happy to help work out the specifics if you need.

Go through the following link
http://xl.barasch.com/cCo11432.htm
and you will able to find the answer

Related

Convert the string to two string by specific link

I have string like below
123443GH
I want to slice it from end to seperate into two strings
For example if my input is 2 , It must return two arrays 123443 and GH , if my input is 3 it must return 12344 and 3GH
Rightnow I use two different codes , Is there any other fastest way to do it ?
str.slice(0,-2)
and
str.slice(-2)
I ran this through a quick benchmark.
It looks like providing the end-index is faster than not.
See here: https://www.measurethat.net/Benchmarks/Show/7513/0/slice-withwithout-end-index

Syntax for a regex for numbers prefixed by a particular string [duplicate]

This question already has answers here:
How to find a number in a string using JavaScript?
(9 answers)
Closed 3 years ago.
I really didn't want to ask this here as I'm sure this will get me downvoted, but I truly am stuck.
I have used multiple tools to test regex expressions but the syntax is very confusing.
What I have tried
I am going to be using javascript for this, I have the following string for example:
Product ID: 4381 - Fanta Berry cans 355ml x 24
This is a search result from an autocomplete dropdown, it will always have the format:
Product ID: product_id - Product Name
Now I need to get the product_id the number between the : and -
I have tried
/[\d]/g
But that simply selects all the numbers in the string.
I also tried:
[(:\b)-]
And that selects the : and - which are the characters between the number I want to get. But I can't seem to figure out the syntax to get the number between them. I feel like I'm very close but after hours of searching I can't seem to crack it, I know this isn't a place for people to do the work for you, but I assure you I have tried and am really at a loss! If anyone can tell me the little bit of syntax that's missing to get that number I would be very appreciative.
You can use a positive lookbehind:
(?<=(Product ID: ))(\d*)
Mind that this method is support by most, but not every browser.
Try specifying the product id prefix:
/Product ID: (\d+)/g
Here's full JavaScript code for you so that you don't get confused:
var str = "Product ID: 4381 - Fanta Berry cans 355ml x 24";
var ret = str.match(/Product ID: (\d+)/);
var product_id = ret[1];
console.log(product_id);
You can use /\d+/ just fine:
const string = "Product ID: 4381 - Fanta Berry cans 355ml x 24";
const match = string.match( /\d+/ )[0];
console.log( match );
Just make sure to select the first element of the resulting array.
Also note that not using the global flag will have the RegEx only match the first result

How to extract only the year from a date string? [duplicate]

This question already has answers here:
Keep only first n characters in a string?
(7 answers)
Closed 3 years ago.
Is there a way to grab only the first four characters of a date string from an API in JavaScript?
Back story
I am using The Movie Database, which allows me to get the exact release dates of movies. For instances, I get the following object of data for "Finding Nemo":
I've added the following code in my Movies.js component, <p>({movie.release_date})</p>
and it returns 2003-05-30 as expected.
But is there a way to extract only the first four characters (2003) and return only that? Or better yet, is there a way to extract only the release year?
There are a few ways you can do this, each method is fairly similar. The below are fairly simple and straight forward to use.
Split at '-'
const date = "2020-01-01";
const year = date.split("-")[0];
alert(year);
This method separates the string where ever there is a '-' and stores each section in an array. You want the first section, the one located in the first spot or with an index of 0. This is probably the easiest and most straight forward method, you can learn more about it here
Slice method
const date = "2020-01-01";
const year = date.slice(0, 4);
alert(year);
This method works by slicing the characters from an index of 0 to an index of 4 off of the string returning them in the constant year.
Substr method
This method was already referenced in this thread by #larz so I won't bother adding an example or explaining it as they can be found in their answer.
I hope this helps, for more information or you want to explore more methods take a look at this link
You can use the substring function on a string, providing the character to start with (0 is the first, like an array) and how many characters to grab.
const date = "2020-01-01";
const year = date.substring(0, 4);
alert(year);
You can achieve extracting only the release year in the following way
const d = new Date( movie.release_date );
d.getFullYear();

How to force an integer into two digits exactly in JavaScript?

The problem is from my trying to convert number into a time format. I'd like to put time like "8:5" into "08:05". Any elegant JavaScript code?
Use split, map, slice and join
"8:5".split(":").map((s)=>("0"+s).slice(-2)).join(":"); //prints 08:05
You can invoke this at the blur/change event of your input box.

JavaScript Concat

Via jquery / Javascript
Given a number 1234567891234
How can I concat, or truncast By removing the 2 from the left to make that number a valid integer I can insert into MySQL:'
4567891234
Thanks
This question edited by someone other than the OP, who's trying to guess at what the OP wants. Bear this in mind, but I think he wants to know:
How can I remove the first two characters from a string, using jQuery?
Assuming they want a substring of the original number:
var result = String(1234567891234).substr(2);
console.log(result); // Should be 34567891234
A crossbrowser way:
(1234567891234).toString().slice(2);

Categories

Resources