I am trying to extract different date formats from a text and later compare those dates if they belong in some time span. Let' say time span is from 1.1.2013 to 1.3.2013 . This is DD/MM/YYYY time format.
Now how can I extract different time formats from text. I have here examples of time formats.
Tue Oct 23, 2012 7:59 am
February 19th, 2013, 07:32 PM
Today, 09:22 PM
Yesterday, 09:03 AM
28 February 2013 09:38
Yesterday 16:48
8 Oct 2012 5:41:00 AM
02-18-2013, 03:17 PM
02-01-13, 12:31 PM
12.2.2013 20:43
I understand this is not a simple task to perform but any kind of suggestion can help me.
Also I am aware of this question and answers . This will benefit me later
Compare two dates with JavaScript
Also the guys on chat had this to say.
Uwe Günther
#IceD looks like you need some lib who implements that all :-)
But ih ave noe clue which one does. Better ask the Stack
IMPORTANT
I don't want jQuery in this since I can't implement it in what I am using right now. So only JS solutions for this.
You will need to normalize them to a common, comparable format, most suitably Date objects.
For some of your formats, (some browsers) will be able to Date.parse them, but for others (like "yesterday") you need to do more sophisticated parsing (up to NLP?). It would be best if you knew the format of each snippet so you can pass them to the right parsing algorithm, if not you'll need to apply some heuristics.
Related
I'm making booking software for a live show. My performers often send me multiple dates that they're available. I'm dealing with lots of performers and lots of dates where there could be conflicting bookings. I want the app to be able to look at all the dates and find all the conflicts.
I would love it if I could copy and paste their email text into a form field and have jquery find the dates to process them. Is there an existing system for doing this? It would have to be smart. Here are some examples of emails I recieve and the arrays I would like returned from javascript...
I can do: Aug. 29, 2018 Sep. 5, 2018 Sep. 19, 2018
{"2018-08-29","2018-09-05","2018-09-19"}
I'm available 10/9 and 11/2
{"2018-10-09","2018-11-02"}
How about Sept 8 & 15?
{"2018-09-08","2018-09-15"}
Can we do next wednesday and Nov 9?
{"2018-08-29","2018-11-09"}
You formats are way to "open" so finding something that would work for all of these would be tricky. You could look into https://dateparser.io/. They seem to support a wide range of cases as you can see here.
Some examples:
December 15th, 2010, 15th of December, 2010-12-15 ...
Also relative dates:
last friday, today, a day from now etc.
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I have a JavaScript Date which I want to get it into SQL Server datetime field.
The JavaScript time sent over as
Fri Sep 15 2017 00:11:44 GMT-0700 (US Mountain Standard Time)
I want it to be converted in javascript or in C#
I need the date to be like
2017-09-15 23:47:01
OR perhaps
9/15/2017 11:47:01 PM
I need to also convert it to UTC , not sure if that is easier to do in javascript or in C#
Which type of format is my code even in? "Fri Sep 15 .." ??
For UTC I was seeing code like this
var isoDate = new Date('yourdatehere').toISOString();
( I am doing .net core web api, with Angular 4 / Typescript)
As Keith said, working with dates is really hard in javascript, especially considering very different implementations across the browsers. So I will second their opinion: just use moment.js and leave everything to that library. It is really powerful. Using it you can get any date format you can possibly think of. To get "2017-09-15 23:47:01" all you need is the following (please pay attention that qualifiers are case sensitive!):
var fmt = moment(<your_js_date_if_you_have_it>).format("YYYY-MM-DD HH:mm:ss");
You can find all supported qualifiers here.
Apart from formatting this library also allows you to do a lot of manipulations with the dates, compare them, translate dates from local to UTC time zone and back, etc.
new Date
when I use the date function in chrome I get something like this
Thu Jul 16 2015 20:37:47 GMT-0700 (Pacific Daylight Time)
when I do the same function in firefox I get this
2015-07-17T03:29:03.110Z
why are they different? I googled for a bit and search on stack but even the examples they were using to show their problem were comparing two dates of the same format. What I'd really like to know is why are they different now and how can I go about making them the chrome format cross as many browsers as possible?
Thanks
Resolved with moment.js
Firefox is in fact giving a different date string, and not simply displaying is differently as stated by Xufox.
The function I am going to use is
moment(new Date()).format("YYYY MMM D H:mm:ss")
but I will change around the .format, just wanted to get the answer here in case others run into the same problem
EDIT!!!!:
Xufox was right. I was a dummy. I had to manually edit some code in my timeline feed to get it going. I needed to use Date.parse on my generic time string, do my editing to the time, then use new Date. Thanks Xufox.
I am working on a very small JavaScript library that allows users to retrieve content based on date. For this content, the date is just an identifier and timezones are completely irrelevant (think along the lines of a Dilbert flip calendar). The "May 14th" content is the same whether you are in the United States or Australia.
The function for retrieving data currently takes a Date object as a parameter. Within the function, the timezone is ignored. Does this approach make sense, or would it be better to take a timezone-independent identifier (like 2012-01-01) as a parameter instead? With the Date object approach, do I run the risk of returning the wrong data as a result of timezone adjustments browsers make?
How about using Date.getUTC*() functions? UTC time is the same for everyone.
After doing some research, it appears that simply ignoring the timezone information is the best approach. Why? This will always preserve the date and time that were provided to the Date constructor (which is my goal), whereas the getUTC* methods will return altered versions of the date and time. For example, take a look at this node REPL session I ran on a computer in the Eastern Time zone.
> d = new Date(2013, 03, 27, 23, 00, 00)
Sat Apr 27 2013 23:00:00 GMT-0400 (EDT)
> d.getDate() // The same date provided in the constructor. Woo!
27
> d.getUTCDate() // A different date. Boo!
28
Long story short, if you want to read the exact date and time that were provided in the Date constructor, using the normal get* methods (like getDate) will do that. If you use the getUTC* methods (like getUTCDate) modified versions of the date and time will be returned.
I know this may sound rudimentary to some of the more experienced programmers out there, but this really helped me make sense of things. I hope it helps others who come along.
The only problem with the approach in your own answer is that it doesn't account for ambiguous times. This happens during daylight savings fall-back transitions.
For example, set your computer's time zone to US Mountain Time ("Mountain Time (US & Canada)" on windows, or "America/Denver" on mac/linux). Then restart your browser and run the following javascript:
var dt = new Date(2013,10,3,1,0);
alert(dt);
This is November 3rd, 2013 at 1:00 AM. But you don't know which 1:00 AM it is representing. Is it in Mountain Daylight Time (UTC-6) before the transition, or Mountain Standard Time (UTC-7) after? There's no way to tell, and JavaScript will just use the standard time.
Now if all you need is 2013-11-03 01:00 - then you are correct. You can just ignore the offset and be done with it. But if you are going to use that value for anything meaningful - such as recording a point in time, or subtracting from another point in time for duration between them, then you have a problem that you can't resolve without the offset.
Unfortunately, there is no great solution for this problem in JavaScript. The closest thing is Moment.js, but it is still not perfect yet. Still, it is better than the Date object by itself, because it gets around browser inconsistencies and provides better parsing and formatting.
Has someone validated date of this format
Feb 9, 2010 12:07 PM
in javascript previously.There is a problem with javascript Date object its accepting Feb 9,2010 00:07 PM as a valid date.
If you fancy using a library, date.js is designed to handle this sort of situation.
Meridiem has its roots in Latin meaning noon or midday. So saying 00:07 PM, or in other words 7 minutes past noon is technically correct though not commonly used.
On the other hand, saying 12 PM is then incorrect as it means 12 hours past noon, or midnight.
So I guess it's more convention than anything, but 00:07 PM is indeed correct. Also, testing on Chrome correctly flags values over 12, such as 13:07 PM as an invalid date.
You might find this section on Confusion at noon and midnight in a 12 hour clock useful.
The behavior of the Date constructor is unspecified, so even if you can explain this behavior, you cannot rely on it working the same way across other browsers. If you want reliable behavior, you should use a library like datejs or dojo.
00 PM is noon. Seems fine to me. It won't accept any hours >12 when using AM/PM so it appears to be intended.