Removing Character conditionally with specific position in JavascriptWhy JavaScript treats a number as octal if it has a leading zeroHow do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Noun clause (singular all the time?)
Combinable filters
Is there an official tutorial for installing Ubuntu 18.04+ on a device with an SSD and an additional internal hard drive?
How to pronounce 'C++' in Spanish
How can the Zone of Truth spell be defeated without the caster knowing?
Error message with tabularx
What does KSP mean?
What are the potential pitfalls when using metals as a currency?
Reducing vertical space in stackrel
The Defining Moment
What does it mean to express a gate in Dirac notation?
How could Tony Stark make this in Endgame?
Please, smoke with good manners
What language was spoken in East Asia before Proto-Turkic?
Binary Numbers Magic Trick
How to verbalise code in Mathematica?
A Note on N!
What does the "ep" capability mean?
Will tsunami waves travel forever if there was no land?
Does Gita support doctrine of eternal cycle of birth and death for evil people?
To say I met a person for the first time
How exactly does Hawking radiation decrease the mass of black holes?
Is there really no use for MD5 anymore?
How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?
Removing Character conditionally with specific position in Javascript
Why JavaScript treats a number as octal if it has a leading zeroHow do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This is my data format:
"21/03/2019 19:18"
The problem i am facing is, when ever if i am dealing with date or time there is an issue with the month ( it has 03 instead of 3 ). I am using library called date-fns. And also i have tried with the help of javascript date objects without using library, but no luck still the month should not have zero in-front of it.
So, how to remove the "0" in-front of "3", and one more problem is how to do this conditionally , because when its Dec, i will be getting data as "21/12/2019 19:18". So, in this case , i should not remove "1" as its located in same position of "0" in previous scenario.
In other words, i want to remove "0" by checking if there is "1" presented in that position or index, if presented then remove else remove "0"
How to achieve this.
I tried the below code:
const d = new Date(2019,03,21)
But, its says legacy error. So when i removed "0" infront of "3" it works fine. Please help
javascript date time
add a comment |
This is my data format:
"21/03/2019 19:18"
The problem i am facing is, when ever if i am dealing with date or time there is an issue with the month ( it has 03 instead of 3 ). I am using library called date-fns. And also i have tried with the help of javascript date objects without using library, but no luck still the month should not have zero in-front of it.
So, how to remove the "0" in-front of "3", and one more problem is how to do this conditionally , because when its Dec, i will be getting data as "21/12/2019 19:18". So, in this case , i should not remove "1" as its located in same position of "0" in previous scenario.
In other words, i want to remove "0" by checking if there is "1" presented in that position or index, if presented then remove else remove "0"
How to achieve this.
I tried the below code:
const d = new Date(2019,03,21)
But, its says legacy error. So when i removed "0" infront of "3" it works fine. Please help
javascript date time
That code snippet runs just fine for me, no errors.new Date(2019,03,21)
andnew Date(2019,3,21)
(no leading zero) produce the exact same date object.
– Chris Barr
Mar 22 at 17:35
Hard to tell what you are asking. Your question should show what you tried, what output you received and what you expected.
– Juan Mendes
Mar 22 at 17:37
Careful about your example date. TheDate
object in JavaScript uses numeric months 0-11, not 1-12. Sonew Date(2019,03,21)
is April 21st.
– Matt Johnson
Mar 22 at 17:59
The error i am getting is "Legacy octal literals are not allowed in strict mode" but when i remove zero, its works fine @JuanMendes
– Dazzile Pro
Mar 22 at 18:01
1
Sure. The leading zero tells JS that the number is octal instead of decimal. It just so happens that 03 is the same in both. JS will allow 08 and 09 and treat those as decimals, but if you were to do 010, you'd see that as decimal 8. In strict mode, this is prohibited. See stackoverflow.com/questions/37003770/… and developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Matt Johnson
Mar 22 at 18:20
add a comment |
This is my data format:
"21/03/2019 19:18"
The problem i am facing is, when ever if i am dealing with date or time there is an issue with the month ( it has 03 instead of 3 ). I am using library called date-fns. And also i have tried with the help of javascript date objects without using library, but no luck still the month should not have zero in-front of it.
So, how to remove the "0" in-front of "3", and one more problem is how to do this conditionally , because when its Dec, i will be getting data as "21/12/2019 19:18". So, in this case , i should not remove "1" as its located in same position of "0" in previous scenario.
In other words, i want to remove "0" by checking if there is "1" presented in that position or index, if presented then remove else remove "0"
How to achieve this.
I tried the below code:
const d = new Date(2019,03,21)
But, its says legacy error. So when i removed "0" infront of "3" it works fine. Please help
javascript date time
This is my data format:
"21/03/2019 19:18"
The problem i am facing is, when ever if i am dealing with date or time there is an issue with the month ( it has 03 instead of 3 ). I am using library called date-fns. And also i have tried with the help of javascript date objects without using library, but no luck still the month should not have zero in-front of it.
So, how to remove the "0" in-front of "3", and one more problem is how to do this conditionally , because when its Dec, i will be getting data as "21/12/2019 19:18". So, in this case , i should not remove "1" as its located in same position of "0" in previous scenario.
In other words, i want to remove "0" by checking if there is "1" presented in that position or index, if presented then remove else remove "0"
How to achieve this.
I tried the below code:
const d = new Date(2019,03,21)
But, its says legacy error. So when i removed "0" infront of "3" it works fine. Please help
javascript date time
javascript date time
edited Mar 22 at 18:03
Dazzile Pro
asked Mar 22 at 17:32
Dazzile ProDazzile Pro
877
877
That code snippet runs just fine for me, no errors.new Date(2019,03,21)
andnew Date(2019,3,21)
(no leading zero) produce the exact same date object.
– Chris Barr
Mar 22 at 17:35
Hard to tell what you are asking. Your question should show what you tried, what output you received and what you expected.
– Juan Mendes
Mar 22 at 17:37
Careful about your example date. TheDate
object in JavaScript uses numeric months 0-11, not 1-12. Sonew Date(2019,03,21)
is April 21st.
– Matt Johnson
Mar 22 at 17:59
The error i am getting is "Legacy octal literals are not allowed in strict mode" but when i remove zero, its works fine @JuanMendes
– Dazzile Pro
Mar 22 at 18:01
1
Sure. The leading zero tells JS that the number is octal instead of decimal. It just so happens that 03 is the same in both. JS will allow 08 and 09 and treat those as decimals, but if you were to do 010, you'd see that as decimal 8. In strict mode, this is prohibited. See stackoverflow.com/questions/37003770/… and developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Matt Johnson
Mar 22 at 18:20
add a comment |
That code snippet runs just fine for me, no errors.new Date(2019,03,21)
andnew Date(2019,3,21)
(no leading zero) produce the exact same date object.
– Chris Barr
Mar 22 at 17:35
Hard to tell what you are asking. Your question should show what you tried, what output you received and what you expected.
– Juan Mendes
Mar 22 at 17:37
Careful about your example date. TheDate
object in JavaScript uses numeric months 0-11, not 1-12. Sonew Date(2019,03,21)
is April 21st.
– Matt Johnson
Mar 22 at 17:59
The error i am getting is "Legacy octal literals are not allowed in strict mode" but when i remove zero, its works fine @JuanMendes
– Dazzile Pro
Mar 22 at 18:01
1
Sure. The leading zero tells JS that the number is octal instead of decimal. It just so happens that 03 is the same in both. JS will allow 08 and 09 and treat those as decimals, but if you were to do 010, you'd see that as decimal 8. In strict mode, this is prohibited. See stackoverflow.com/questions/37003770/… and developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Matt Johnson
Mar 22 at 18:20
That code snippet runs just fine for me, no errors.
new Date(2019,03,21)
and new Date(2019,3,21)
(no leading zero) produce the exact same date object.– Chris Barr
Mar 22 at 17:35
That code snippet runs just fine for me, no errors.
new Date(2019,03,21)
and new Date(2019,3,21)
(no leading zero) produce the exact same date object.– Chris Barr
Mar 22 at 17:35
Hard to tell what you are asking. Your question should show what you tried, what output you received and what you expected.
– Juan Mendes
Mar 22 at 17:37
Hard to tell what you are asking. Your question should show what you tried, what output you received and what you expected.
– Juan Mendes
Mar 22 at 17:37
Careful about your example date. The
Date
object in JavaScript uses numeric months 0-11, not 1-12. So new Date(2019,03,21)
is April 21st.– Matt Johnson
Mar 22 at 17:59
Careful about your example date. The
Date
object in JavaScript uses numeric months 0-11, not 1-12. So new Date(2019,03,21)
is April 21st.– Matt Johnson
Mar 22 at 17:59
The error i am getting is "Legacy octal literals are not allowed in strict mode" but when i remove zero, its works fine @JuanMendes
– Dazzile Pro
Mar 22 at 18:01
The error i am getting is "Legacy octal literals are not allowed in strict mode" but when i remove zero, its works fine @JuanMendes
– Dazzile Pro
Mar 22 at 18:01
1
1
Sure. The leading zero tells JS that the number is octal instead of decimal. It just so happens that 03 is the same in both. JS will allow 08 and 09 and treat those as decimals, but if you were to do 010, you'd see that as decimal 8. In strict mode, this is prohibited. See stackoverflow.com/questions/37003770/… and developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Matt Johnson
Mar 22 at 18:20
Sure. The leading zero tells JS that the number is octal instead of decimal. It just so happens that 03 is the same in both. JS will allow 08 and 09 and treat those as decimals, but if you were to do 010, you'd see that as decimal 8. In strict mode, this is prohibited. See stackoverflow.com/questions/37003770/… and developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Matt Johnson
Mar 22 at 18:20
add a comment |
2 Answers
2
active
oldest
votes
I assume you get the data back as a string and you just want to remove leading zeros from the 2nd number only?
we can use .split
to break up the string into parts, and then we can use parseInt
to convert some string parts into numbers. that will turn the string "03"
into the number 3
function removeleadingZerosFromDateString(str)
//Break up the date string on the slashes and whitespace, so we have an array of all the parts
var parts = str.split(//
var result = removeleadingZerosFromDateString("21/03/2019 19:18 PM");
console.log(result);
So parseInt automatically assumes 03 as 3 ? what is the date is like "21/11/2019"
– Dazzile Pro
Mar 22 at 17:47
Careful - Months on theDate
object are 0-11. You need to subtract one from the parsed value.
– Matt Johnson
Mar 22 at 17:49
parseInt
converts strings to numbers, and since leading zeros don't exist in numbers, it removes them. in math00000003
and3
are equal.
– Chris Barr
Mar 22 at 17:54
@MattJohnson true, but there's no requirement (that I could tell anyway) to use aDate
object. They just want to remove leading zeros from a certain position in a string
– Chris Barr
Mar 22 at 17:55
Well, if you need to use a date Object, then you need to do something else entirely. My answer is just about removing a 0.
– Chris Barr
Mar 22 at 17:56
|
show 1 more comment
You said you were using date-fns, so I'll give an answer in that regard.
The current 1.x version doesn't support parsing strings in a custom format, but they are adding that to 2.x, and you can use the alpha release to try it today.
The syntax is:
var date = parse(dateString, formatString, baseDate, [options]);
See the documentation for the parse
function in version 2.0.0-alpha.27
.
In your case, it would be like this:
var date = parse("21/03/2019 19:18", "MM/dd/yyyy HH:mm", new Date());
Lastly, if you want to use a library for this but don't want to experiment with an alpha, you can either wait for Date-fns 2.0 to become final, or you can try Luxon or Moment - both of which already have this functionality (though Moment uses a slightly different token format "MM/DD/YYYY HH:mm"
).
The Pm was an typo , sorry for that ! And understood
– Dazzile Pro
Mar 22 at 18:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304987%2fremoving-character-conditionally-with-specific-position-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I assume you get the data back as a string and you just want to remove leading zeros from the 2nd number only?
we can use .split
to break up the string into parts, and then we can use parseInt
to convert some string parts into numbers. that will turn the string "03"
into the number 3
function removeleadingZerosFromDateString(str)
//Break up the date string on the slashes and whitespace, so we have an array of all the parts
var parts = str.split(//
var result = removeleadingZerosFromDateString("21/03/2019 19:18 PM");
console.log(result);
So parseInt automatically assumes 03 as 3 ? what is the date is like "21/11/2019"
– Dazzile Pro
Mar 22 at 17:47
Careful - Months on theDate
object are 0-11. You need to subtract one from the parsed value.
– Matt Johnson
Mar 22 at 17:49
parseInt
converts strings to numbers, and since leading zeros don't exist in numbers, it removes them. in math00000003
and3
are equal.
– Chris Barr
Mar 22 at 17:54
@MattJohnson true, but there's no requirement (that I could tell anyway) to use aDate
object. They just want to remove leading zeros from a certain position in a string
– Chris Barr
Mar 22 at 17:55
Well, if you need to use a date Object, then you need to do something else entirely. My answer is just about removing a 0.
– Chris Barr
Mar 22 at 17:56
|
show 1 more comment
I assume you get the data back as a string and you just want to remove leading zeros from the 2nd number only?
we can use .split
to break up the string into parts, and then we can use parseInt
to convert some string parts into numbers. that will turn the string "03"
into the number 3
function removeleadingZerosFromDateString(str)
//Break up the date string on the slashes and whitespace, so we have an array of all the parts
var parts = str.split(//
var result = removeleadingZerosFromDateString("21/03/2019 19:18 PM");
console.log(result);
So parseInt automatically assumes 03 as 3 ? what is the date is like "21/11/2019"
– Dazzile Pro
Mar 22 at 17:47
Careful - Months on theDate
object are 0-11. You need to subtract one from the parsed value.
– Matt Johnson
Mar 22 at 17:49
parseInt
converts strings to numbers, and since leading zeros don't exist in numbers, it removes them. in math00000003
and3
are equal.
– Chris Barr
Mar 22 at 17:54
@MattJohnson true, but there's no requirement (that I could tell anyway) to use aDate
object. They just want to remove leading zeros from a certain position in a string
– Chris Barr
Mar 22 at 17:55
Well, if you need to use a date Object, then you need to do something else entirely. My answer is just about removing a 0.
– Chris Barr
Mar 22 at 17:56
|
show 1 more comment
I assume you get the data back as a string and you just want to remove leading zeros from the 2nd number only?
we can use .split
to break up the string into parts, and then we can use parseInt
to convert some string parts into numbers. that will turn the string "03"
into the number 3
function removeleadingZerosFromDateString(str)
//Break up the date string on the slashes and whitespace, so we have an array of all the parts
var parts = str.split(//
var result = removeleadingZerosFromDateString("21/03/2019 19:18 PM");
console.log(result);
I assume you get the data back as a string and you just want to remove leading zeros from the 2nd number only?
we can use .split
to break up the string into parts, and then we can use parseInt
to convert some string parts into numbers. that will turn the string "03"
into the number 3
function removeleadingZerosFromDateString(str)
//Break up the date string on the slashes and whitespace, so we have an array of all the parts
var parts = str.split(//
var result = removeleadingZerosFromDateString("21/03/2019 19:18 PM");
console.log(result);
function removeleadingZerosFromDateString(str)
//Break up the date string on the slashes and whitespace, so we have an array of all the parts
var parts = str.split(//
var result = removeleadingZerosFromDateString("21/03/2019 19:18 PM");
console.log(result);
function removeleadingZerosFromDateString(str)
//Break up the date string on the slashes and whitespace, so we have an array of all the parts
var parts = str.split(//
var result = removeleadingZerosFromDateString("21/03/2019 19:18 PM");
console.log(result);
answered Mar 22 at 17:43
Chris BarrChris Barr
12.4k1466104
12.4k1466104
So parseInt automatically assumes 03 as 3 ? what is the date is like "21/11/2019"
– Dazzile Pro
Mar 22 at 17:47
Careful - Months on theDate
object are 0-11. You need to subtract one from the parsed value.
– Matt Johnson
Mar 22 at 17:49
parseInt
converts strings to numbers, and since leading zeros don't exist in numbers, it removes them. in math00000003
and3
are equal.
– Chris Barr
Mar 22 at 17:54
@MattJohnson true, but there's no requirement (that I could tell anyway) to use aDate
object. They just want to remove leading zeros from a certain position in a string
– Chris Barr
Mar 22 at 17:55
Well, if you need to use a date Object, then you need to do something else entirely. My answer is just about removing a 0.
– Chris Barr
Mar 22 at 17:56
|
show 1 more comment
So parseInt automatically assumes 03 as 3 ? what is the date is like "21/11/2019"
– Dazzile Pro
Mar 22 at 17:47
Careful - Months on theDate
object are 0-11. You need to subtract one from the parsed value.
– Matt Johnson
Mar 22 at 17:49
parseInt
converts strings to numbers, and since leading zeros don't exist in numbers, it removes them. in math00000003
and3
are equal.
– Chris Barr
Mar 22 at 17:54
@MattJohnson true, but there's no requirement (that I could tell anyway) to use aDate
object. They just want to remove leading zeros from a certain position in a string
– Chris Barr
Mar 22 at 17:55
Well, if you need to use a date Object, then you need to do something else entirely. My answer is just about removing a 0.
– Chris Barr
Mar 22 at 17:56
So parseInt automatically assumes 03 as 3 ? what is the date is like "21/11/2019"
– Dazzile Pro
Mar 22 at 17:47
So parseInt automatically assumes 03 as 3 ? what is the date is like "21/11/2019"
– Dazzile Pro
Mar 22 at 17:47
Careful - Months on the
Date
object are 0-11. You need to subtract one from the parsed value.– Matt Johnson
Mar 22 at 17:49
Careful - Months on the
Date
object are 0-11. You need to subtract one from the parsed value.– Matt Johnson
Mar 22 at 17:49
parseInt
converts strings to numbers, and since leading zeros don't exist in numbers, it removes them. in math 00000003
and 3
are equal.– Chris Barr
Mar 22 at 17:54
parseInt
converts strings to numbers, and since leading zeros don't exist in numbers, it removes them. in math 00000003
and 3
are equal.– Chris Barr
Mar 22 at 17:54
@MattJohnson true, but there's no requirement (that I could tell anyway) to use a
Date
object. They just want to remove leading zeros from a certain position in a string– Chris Barr
Mar 22 at 17:55
@MattJohnson true, but there's no requirement (that I could tell anyway) to use a
Date
object. They just want to remove leading zeros from a certain position in a string– Chris Barr
Mar 22 at 17:55
Well, if you need to use a date Object, then you need to do something else entirely. My answer is just about removing a 0.
– Chris Barr
Mar 22 at 17:56
Well, if you need to use a date Object, then you need to do something else entirely. My answer is just about removing a 0.
– Chris Barr
Mar 22 at 17:56
|
show 1 more comment
You said you were using date-fns, so I'll give an answer in that regard.
The current 1.x version doesn't support parsing strings in a custom format, but they are adding that to 2.x, and you can use the alpha release to try it today.
The syntax is:
var date = parse(dateString, formatString, baseDate, [options]);
See the documentation for the parse
function in version 2.0.0-alpha.27
.
In your case, it would be like this:
var date = parse("21/03/2019 19:18", "MM/dd/yyyy HH:mm", new Date());
Lastly, if you want to use a library for this but don't want to experiment with an alpha, you can either wait for Date-fns 2.0 to become final, or you can try Luxon or Moment - both of which already have this functionality (though Moment uses a slightly different token format "MM/DD/YYYY HH:mm"
).
The Pm was an typo , sorry for that ! And understood
– Dazzile Pro
Mar 22 at 18:02
add a comment |
You said you were using date-fns, so I'll give an answer in that regard.
The current 1.x version doesn't support parsing strings in a custom format, but they are adding that to 2.x, and you can use the alpha release to try it today.
The syntax is:
var date = parse(dateString, formatString, baseDate, [options]);
See the documentation for the parse
function in version 2.0.0-alpha.27
.
In your case, it would be like this:
var date = parse("21/03/2019 19:18", "MM/dd/yyyy HH:mm", new Date());
Lastly, if you want to use a library for this but don't want to experiment with an alpha, you can either wait for Date-fns 2.0 to become final, or you can try Luxon or Moment - both of which already have this functionality (though Moment uses a slightly different token format "MM/DD/YYYY HH:mm"
).
The Pm was an typo , sorry for that ! And understood
– Dazzile Pro
Mar 22 at 18:02
add a comment |
You said you were using date-fns, so I'll give an answer in that regard.
The current 1.x version doesn't support parsing strings in a custom format, but they are adding that to 2.x, and you can use the alpha release to try it today.
The syntax is:
var date = parse(dateString, formatString, baseDate, [options]);
See the documentation for the parse
function in version 2.0.0-alpha.27
.
In your case, it would be like this:
var date = parse("21/03/2019 19:18", "MM/dd/yyyy HH:mm", new Date());
Lastly, if you want to use a library for this but don't want to experiment with an alpha, you can either wait for Date-fns 2.0 to become final, or you can try Luxon or Moment - both of which already have this functionality (though Moment uses a slightly different token format "MM/DD/YYYY HH:mm"
).
You said you were using date-fns, so I'll give an answer in that regard.
The current 1.x version doesn't support parsing strings in a custom format, but they are adding that to 2.x, and you can use the alpha release to try it today.
The syntax is:
var date = parse(dateString, formatString, baseDate, [options]);
See the documentation for the parse
function in version 2.0.0-alpha.27
.
In your case, it would be like this:
var date = parse("21/03/2019 19:18", "MM/dd/yyyy HH:mm", new Date());
Lastly, if you want to use a library for this but don't want to experiment with an alpha, you can either wait for Date-fns 2.0 to become final, or you can try Luxon or Moment - both of which already have this functionality (though Moment uses a slightly different token format "MM/DD/YYYY HH:mm"
).
edited Mar 22 at 18:18
answered Mar 22 at 17:56
Matt JohnsonMatt Johnson
144k42290415
144k42290415
The Pm was an typo , sorry for that ! And understood
– Dazzile Pro
Mar 22 at 18:02
add a comment |
The Pm was an typo , sorry for that ! And understood
– Dazzile Pro
Mar 22 at 18:02
The Pm was an typo , sorry for that ! And understood
– Dazzile Pro
Mar 22 at 18:02
The Pm was an typo , sorry for that ! And understood
– Dazzile Pro
Mar 22 at 18:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304987%2fremoving-character-conditionally-with-specific-position-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
That code snippet runs just fine for me, no errors.
new Date(2019,03,21)
andnew Date(2019,3,21)
(no leading zero) produce the exact same date object.– Chris Barr
Mar 22 at 17:35
Hard to tell what you are asking. Your question should show what you tried, what output you received and what you expected.
– Juan Mendes
Mar 22 at 17:37
Careful about your example date. The
Date
object in JavaScript uses numeric months 0-11, not 1-12. Sonew Date(2019,03,21)
is April 21st.– Matt Johnson
Mar 22 at 17:59
The error i am getting is "Legacy octal literals are not allowed in strict mode" but when i remove zero, its works fine @JuanMendes
– Dazzile Pro
Mar 22 at 18:01
1
Sure. The leading zero tells JS that the number is octal instead of decimal. It just so happens that 03 is the same in both. JS will allow 08 and 09 and treat those as decimals, but if you were to do 010, you'd see that as decimal 8. In strict mode, this is prohibited. See stackoverflow.com/questions/37003770/… and developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Matt Johnson
Mar 22 at 18:20