Converting a full date string like “Saturday 23rd March 2019” to Javascript Date()How do you get a timestamp in JavaScript?var functionName = function() vs function functionName() Where can I find documentation on formatting a date in JavaScript?Convert a string to an integer in JavaScript?How to replace all occurrences of a string in JavaScriptConvert form data to JavaScript object with jQueryDetecting an “invalid date” Date instance in JavaScriptevent.preventDefault() vs. return falseStoring Objects in HTML5 localStorageConverting a string to a date in JavaScript
How to laser-level close to a surface
Quotient of Three Dimensional Torus by Permutation on Coordinates
Will this series of events work to drown the Tarrasque?
Would a "ring language" be possible?
Should I twist DC power and ground wires from a power supply?
Bookshelves: the intruder
Is it possible to determine from only a photo of a cityscape whether it was taken close with wide angle or from a distance with zoom?
Alternative classical explanation of the Stern-Gerlach Experiment?
Is it standard to have the first week's pay indefinitely withheld?
Why we learn compiler?
Are there any symmetric cryptosystems based on computational complexity assumptions?
What should I wear to go and sign an employment contract?
When did Britain learn about the American Declaration of Independence?
Is there a language that let's you use a try block without a catch block?
How do I balance a campaign consisting of four kobold PCs?
How to customize the pie chart background in PowerPoint?
Divisor Rich and Poor Numbers
Parse a C++14 integer literal
Can an airline pilot be prosecuted for killing an unruly passenger who could not be physically restrained?
Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario
Have GoT's showrunners reacted to the poor reception of the final season?
What is the probability that two cards drawn from a deck are both face cards and at least one is red?
Why are there five extra turns in tournament Magic?
Hotel booking: Why is Agoda much cheaper than booking.com?
Converting a full date string like “Saturday 23rd March 2019” to Javascript Date()
How do you get a timestamp in JavaScript?var functionName = function() vs function functionName() Where can I find documentation on formatting a date in JavaScript?Convert a string to an integer in JavaScript?How to replace all occurrences of a string in JavaScriptConvert form data to JavaScript object with jQueryDetecting an “invalid date” Date instance in JavaScriptevent.preventDefault() vs. return falseStoring Objects in HTML5 localStorageConverting a string to a date in JavaScript
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
How can I convert a string like "Saturday 23rd March 2019" to Javascript date?
For some reason it says invalid date and other questions similar to this are trying to convert strings like "23-03-2019 10:00" etc to Date, whereas this is more of a full date string.
javascript date data-conversion
add a comment |
How can I convert a string like "Saturday 23rd March 2019" to Javascript date?
For some reason it says invalid date and other questions similar to this are trying to convert strings like "23-03-2019 10:00" etc to Date, whereas this is more of a full date string.
javascript date data-conversion
add a comment |
How can I convert a string like "Saturday 23rd March 2019" to Javascript date?
For some reason it says invalid date and other questions similar to this are trying to convert strings like "23-03-2019 10:00" etc to Date, whereas this is more of a full date string.
javascript date data-conversion
How can I convert a string like "Saturday 23rd March 2019" to Javascript date?
For some reason it says invalid date and other questions similar to this are trying to convert strings like "23-03-2019 10:00" etc to Date, whereas this is more of a full date string.
javascript date data-conversion
javascript date data-conversion
asked Mar 23 at 17:36
ScoopScoop
304
304
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It's actually the "rd" of 23rd that's causing the issue. Otherwise the string as is will convert to a date. I've used a regex replace to substitute "23rd" with "23". Also tossed in another example to capture "1st" as well.
var dateString = "Saturday 23rd March 2019";
var cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
dateString = "Friday 1st March 2019";
cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
If you're going to the trouble of modifying the string to give it to a parser, you might as well just parse it yourself, it's just 3 lines of code. :-)
– RobG
Mar 23 at 21:26
add a comment |
Best bet would be use a library like moment js to parse that string and tell it the format expected :
const str = "Saturday 23rd March 2019";
const m = moment(str, "dddd Do MMMM YYYY");
console.log(m.toDate())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
If the formatting is not consistent you will need to write a custom parser which won't be a trivial exercise.
"If the formatting is not consistent…" then any parser will struggle, as evidenced by the vagaries of built–in parsers. ;-)
– RobG
Mar 23 at 21:24
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%2f55316542%2fconverting-a-full-date-string-like-saturday-23rd-march-2019-to-javascript-date%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
It's actually the "rd" of 23rd that's causing the issue. Otherwise the string as is will convert to a date. I've used a regex replace to substitute "23rd" with "23". Also tossed in another example to capture "1st" as well.
var dateString = "Saturday 23rd March 2019";
var cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
dateString = "Friday 1st March 2019";
cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
If you're going to the trouble of modifying the string to give it to a parser, you might as well just parse it yourself, it's just 3 lines of code. :-)
– RobG
Mar 23 at 21:26
add a comment |
It's actually the "rd" of 23rd that's causing the issue. Otherwise the string as is will convert to a date. I've used a regex replace to substitute "23rd" with "23". Also tossed in another example to capture "1st" as well.
var dateString = "Saturday 23rd March 2019";
var cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
dateString = "Friday 1st March 2019";
cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
If you're going to the trouble of modifying the string to give it to a parser, you might as well just parse it yourself, it's just 3 lines of code. :-)
– RobG
Mar 23 at 21:26
add a comment |
It's actually the "rd" of 23rd that's causing the issue. Otherwise the string as is will convert to a date. I've used a regex replace to substitute "23rd" with "23". Also tossed in another example to capture "1st" as well.
var dateString = "Saturday 23rd March 2019";
var cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
dateString = "Friday 1st March 2019";
cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
It's actually the "rd" of 23rd that's causing the issue. Otherwise the string as is will convert to a date. I've used a regex replace to substitute "23rd" with "23". Also tossed in another example to capture "1st" as well.
var dateString = "Saturday 23rd March 2019";
var cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
dateString = "Friday 1st March 2019";
cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
var dateString = "Saturday 23rd March 2019";
var cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
dateString = "Friday 1st March 2019";
cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
var dateString = "Saturday 23rd March 2019";
var cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
dateString = "Friday 1st March 2019";
cleanDateString = dateString.replace(/(d+)(rd|st|nd|th)/, "$1");
console.log(new Date(cleanDateString));
answered Mar 23 at 17:54
Doug FDoug F
8202917
8202917
If you're going to the trouble of modifying the string to give it to a parser, you might as well just parse it yourself, it's just 3 lines of code. :-)
– RobG
Mar 23 at 21:26
add a comment |
If you're going to the trouble of modifying the string to give it to a parser, you might as well just parse it yourself, it's just 3 lines of code. :-)
– RobG
Mar 23 at 21:26
If you're going to the trouble of modifying the string to give it to a parser, you might as well just parse it yourself, it's just 3 lines of code. :-)
– RobG
Mar 23 at 21:26
If you're going to the trouble of modifying the string to give it to a parser, you might as well just parse it yourself, it's just 3 lines of code. :-)
– RobG
Mar 23 at 21:26
add a comment |
Best bet would be use a library like moment js to parse that string and tell it the format expected :
const str = "Saturday 23rd March 2019";
const m = moment(str, "dddd Do MMMM YYYY");
console.log(m.toDate())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
If the formatting is not consistent you will need to write a custom parser which won't be a trivial exercise.
"If the formatting is not consistent…" then any parser will struggle, as evidenced by the vagaries of built–in parsers. ;-)
– RobG
Mar 23 at 21:24
add a comment |
Best bet would be use a library like moment js to parse that string and tell it the format expected :
const str = "Saturday 23rd March 2019";
const m = moment(str, "dddd Do MMMM YYYY");
console.log(m.toDate())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
If the formatting is not consistent you will need to write a custom parser which won't be a trivial exercise.
"If the formatting is not consistent…" then any parser will struggle, as evidenced by the vagaries of built–in parsers. ;-)
– RobG
Mar 23 at 21:24
add a comment |
Best bet would be use a library like moment js to parse that string and tell it the format expected :
const str = "Saturday 23rd March 2019";
const m = moment(str, "dddd Do MMMM YYYY");
console.log(m.toDate())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
If the formatting is not consistent you will need to write a custom parser which won't be a trivial exercise.
Best bet would be use a library like moment js to parse that string and tell it the format expected :
const str = "Saturday 23rd March 2019";
const m = moment(str, "dddd Do MMMM YYYY");
console.log(m.toDate())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
If the formatting is not consistent you will need to write a custom parser which won't be a trivial exercise.
const str = "Saturday 23rd March 2019";
const m = moment(str, "dddd Do MMMM YYYY");
console.log(m.toDate())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
const str = "Saturday 23rd March 2019";
const m = moment(str, "dddd Do MMMM YYYY");
console.log(m.toDate())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
answered Mar 23 at 17:48
charlietflcharlietfl
143k1395126
143k1395126
"If the formatting is not consistent…" then any parser will struggle, as evidenced by the vagaries of built–in parsers. ;-)
– RobG
Mar 23 at 21:24
add a comment |
"If the formatting is not consistent…" then any parser will struggle, as evidenced by the vagaries of built–in parsers. ;-)
– RobG
Mar 23 at 21:24
"If the formatting is not consistent…" then any parser will struggle, as evidenced by the vagaries of built–in parsers. ;-)
– RobG
Mar 23 at 21:24
"If the formatting is not consistent…" then any parser will struggle, as evidenced by the vagaries of built–in parsers. ;-)
– RobG
Mar 23 at 21:24
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%2f55316542%2fconverting-a-full-date-string-like-saturday-23rd-march-2019-to-javascript-date%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