Use moment to check if date is from 2 minutes ago - date

I tried this but I can't make it work.
I have an array of dates like this: ['5/15/2017, 2:59:06 PM', '5/15/2017, 2:59:16 PM', ...]
And I want to filter it and get the dates from the last 2 minutes.
This is what I'm doing:
const twoMinutesAgo = moment().subtract(2, 'minutes')
myArray.filter(stat => moment(stat.date).isAfter(twoMinutesAgo))
but this is returning always true.
I saw that twoMinutesAgo is Mon May 15 2017 14:57:09 GMT-0700 (PDT)
while moment(stat.date) is Mon Mar 05 2018 14:59:06 GMT-0800 (PST)
But I'm not sure if that has something to do with it. Any ideas what I'm doing wrong?

Part of the issue is that you're only looking at the hour of the day, but you're actually comparing absolute dates.
The other issue you have is moment isn't correctly parsing your date strings. To rectify this, you can provide a format string to instruct moment on how to parse apart the important bits:
const PARSE_FORMAT = 'M/D/YYYY, H:mm:ss A';
const twoMinutesAgo = moment().subtract(2, 'minutes')
myArray.filter(stat => moment(stat.date, PARSE_FORMAT).isAfter(twoMinutesAgo));

Related

Extract Dates and compare them from string in Scala

I am trying to extract date from string and compare them. I am new to Scala. The string : Some(Date: Tue, 14 Aug 2018 20:57:42 GMT)Some(Last-Modified: Tue, 14 Aug 2018 20:57:24 GMT) I wish to comapare Date and Last Modified
Extract the Dates if working with Option
There are several Scala wrappers for the Java Time API but the example below just uses the Java API directly.
val someDate: Option[String] = Some("Date: Tue, 14 Aug 2018 20:57:42 GMT")
val someLastMod: Option[String] = Some("Last-Modified: Tue, 14 Aug 2018 20:57:24 GMT")
The we extract the meaningful date substrings ie. we remove the "Date: "
val dateStr = someDate.get.split("^[\\w\\-]+:")(1).trim
val lastModStr = someLastMod.get.split("^[\\w\\-]+:")(1).trim
You should note that the above uses get() which assumes you can guarantee you will always have a Some and never a None. You should read up on working with Option in Scala if you don't understand this point.
Extract the Dates if working with a String
val data = "Some(Date: Tue, 14 Aug 2018 20:57:42 GMT)Some(Last-Modified: Tue, 14 Aug 2018 20:57:24 GMT)"
First we extract just the string dates we are interested in. The following expression uses split to create an array of strings, which we filter over to remove any empty strings before finally mapping over whats left and using take to remove the trailing parenthesis )
val dates = data.split("Some\\([\\w\\-]+:*\\s").filter(_.nonEmpty).map(_.take(29))
// dates: Array[String] = Array(Tue, 14 Aug 2018 20:57:42 GMT, Tue, 14 Aug 2018 20:57:24 GMT)
Now we extract each date string from the array.
val dateStr = dates(0)
val lastModStr = dates(1)
Now use the Java Time API to do comparisons.
Now we start to use the Java time API. First you need to import the Java packages.
import java.time._
import java.time.format._
Now create a formatter to match your DateTime pattern in order to convert the Strings to LocalDateTime instances.
val formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss z")
val date = LocalDateTime.parse(dateStr, formatter)
val lastMod = LocalDateTime.parse(lastModStr, formatter)
Do some comparisons using the LocalDateTime API.
date.isBefore(lastMod)
date.isAfter(lastMod)
Check out the LocalDateTime docs for more ways to compare them.
Consider this
Will the format for the Dates always be in the same pattern? If not, you will need to think about how you will handle different patterns otherwise you will run into runtime exceptions (DateTimeParseException). Read more in the docs
Are you really trying to parse data that looks like this?
val badString = "Some(Date: Tue, 14 Aug 2018 20:57:42 GMT)Some(Last-Modified: Tue, 14 Aug 2018 20:57:24 GMT)"
Whoever thought that might be a reasonable way to represent data should go back to school (grade school). But it can be done. First let's try to segregate the data elements we're interested in, and remove some of the cruft along the way.
val inArray :Array[String] = badString.split("Some[^:]+: ")
//inArray: Array[String] = Array("", "Tue, 14 Aug 2018 20:57:42 GMT)", "Tue, 14 Aug 2018 20:57:24 GMT)")
Next we need to describe the date/time format that we're dealing with. Note that we have to account for a trailing paren ) in the data.
import java.time.format.DateTimeFormatter
val dtFormatter = DateTimeFormatter.ofPattern("E, dd MMM yyyy HH:mm:ss z)")
Now we can turn all the good data into Java LocalDateTime elements. Any Array elements that don't match the DateTimeFormatter pattern are removed.
import util.Try
import java.time.LocalDateTime
val dates :Array[LocalDateTime] = inArray.flatMap{ dateStr =>
Try(LocalDateTime.parse(dateStr.trim, dtFormatter)).toOption
}
So now you can extract the dates, if any, from the dates array and compare them using the LocalDateTime API.

GWT DateTimeFormat returns the wrong date

I have the following code
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("MM/dd/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
And the alert box shows the date
09/04/2014
instead of
21/04/2013
Why?
As others already say, it's because of your pattern. What they don't say is why it behaves that way.
When parsing 21/04/2013 as MM/dd/yyyy, DateTimeFormat will decompose the date as:
Month Day of month Year
21 4 2013
and it'll then adjust things to make a valid date. To do that, the Month part is truncated at 12 (so that temporary date is Dec 4th, 2013) and the remainder (21 - 12 = 9) is then added, leading to Sept. 4th 2014, which according to your format displays as 09/04/2014.
You wanted to show 21/04/2013 but the format was MM/dd/yyyy.
It should be dd/MM/yyyy
So change it like this:
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
You're reversing day and month.
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));

DataJS Rendering Date as /Date(1363708765000)/

I am accessing the SharePoint 2010 List WCF data service via DataJS and getting back date fields as /Date(1363708765000)/ does anyone have any idea how I can process this to display a proper date?
Note: I am posing here as I suspect this is a DataJS question more than a peculiarity of the ListData.svc in SharePoint.
What you're receiving back is a Unix epoch, which represents the number of milliseconds that have elapsed since Jan. 01 1970. Fortunately, JavaScript also uses this as its epoch, meaning you can create a new Date object using elapsed milliseconds as your input parameter:
var myDate = new Date(1363708765000);
console.log("UTC:" + myDate.toUTCString());
// outputs UTC: Tue, 19 Mar 2013 15:59:25 GMT
Of course, you'll have to parse out the integer portion of the date value you're getting back before you can use it to initialize a date. A reusable function might look like this:
function parseJsonDate( sDate ) {
var b, e, i;
b = sDate.indexOf('(');
e = sDate.indexOf(')');
i = sDate.substring(b+1,e);
if (isNaN(i)) { return null };
return new Date(parseInt(i));
}

Conversion between date formats

I have a spreadsheet which we use to log our work. I have read the date's column from the sheet into an array so that I can perform actions/calculations on them. This did not work for me as I noticed in the debugger that the array which holds the dates have values different formats of the date values. For instance, some display as "16/11/2012" and some as (new Date(1355184000000)).
Can someone point a way to convert them all to a unified format so that I can work with them?
Thanks
How do you want your dates to be shown? Do you want them to be 'date objects' or strings? the value you show in your code (new Date(1355184000000)) correspond to Mon Dec 10 16:00:00 PST 2012
You can check that by using Logger.log(new Date(1355184000000))
On the contrary "16/11/2012" is most probably not a date but a string...(note : strange that you use a "day/month/year" sequence since I saw on your profile you are in UK, I thought you'd use mm/dd/yyyy instead).
Since you said you need to make some calculations on these items I guess that they all should be converted to date objects for using them in you script.
I'd suggest you look at some documentation on date object to see exactly how this should be done without generating errors. Don't forget that dates in javascript are always dates and time in hh:mm:ss and milliseconds. The integer value you saw was the number of milliseconds since January the first in 1970 ;-)
You could also do a search on dates in this forum and find quite a lot of interresting informations.
Here is a small function to illustrate :
function playWithTime(){
Logger.log('ref date = '+new Date(0))
var example = "june 30, 2013 23:59:00"
Logger.log(example+' = '+ new Date("june 30, 2013 23:59:00"))
Logger.log(example+' = '+ new Date("june 30, 2013 23:59:00").getTime()+' mS')
}
It will show this in the Logger :
ref date = Thu Jan 01 1970 01:00:00 GMT+0100 (CET)
june 30, 2013 23:59:00 = Sun Jun 30 2013 23:59:00 GMT+0200 (CEST)
june 30, 2013 23:59:00 = 1372629540000 mS
btw, note that the Logger returns values in different ways, depending on the daylight saving in your timezone... I'm in Belgium and june is in 'summer' time (CEST). It can also be shown in PDT or PST which is the timezone of Google servers. You can't rely on the logger to be constant (!!) but that's another story ;-)
EDIT : If your date strings are in the form dd/mm/yyyy then you should probably reorder it like in this code :
function playWithTime2(){
Logger.log('original date string in UK format = 16/11/2012')
var d = "16/11/2012".split('/');
var d_ordered=d[1]+'/'+d[0]+'/'+d[2]
Logger.log('becomes '+d_ordered+' = '+new Date(d_ordered))
}
Which returns
original date string in UK format = 16/11/2012
becomes 11/16/2012 = Fri Nov 16 2012 00:00:00 GMT+0100 (CET)

Calculate difference in months between two dates with Groovy

I need to calculate the difference in months between two dates.
start = new Date(112, 4, 30) // Wed May 30 00:00:00 CEST 2012
end = new Date(111, 9, 11) // Tue Oct 11 00:00:00 CEST 2011
assert 8 == monthsBetween(start, end)
Using Joda-Time it's really easy to achieve this with something like this:
months = Months.monthsBetween(start, end).getMonths()
How can I achieve this in a Groovy way, without using other libraries?
monthBetween = (start[Calendar.MONTH] - end[Calendar.MONTH]) + 1
yearsBetween = start[Calendar.YEAR] - end[Calendar.YEAR]
months = monthBetween + (yearsBetween * 12)
If you want to get difference in months by month name, instead of days and weeks, you can do this.
Ex: December 7, 2013 vs January 21, 2014 will give you a difference of 1
Date dateFrom = Date.parse("yyyy-MM-dd", "2013-12-07")
Date dateTo = Date.parse("yyyy-MM-dd", "2014-01-21")
def diffMonths = { Date start, Date end ->
int diffYears = (start[Calendar.YEAR] - end[Calendar.YEAR]) * 12
int diffMonths = start[Calendar.MONTH] - end[Calendar.MONTH]
return diffYears + diffMonths
}
println diffMonths(dateTo, dateFrom)
The following will output 1
(start[Calendar.MONTH]-end[Calendar.MONTH]+1)+((start[Calendar.YEAR]-end[Calendar.YEAR])*12)
Just for fun (as it probably less readable, and uses more resources), you could also do:
months = (start..end).collect { [ it[ Calendar.YEAR ], it[ Calendar.MONTH ] ] }
.unique()
.size()
I agree with #JonSkeet: you should continue to use Joda-Time. IMO, Joda-Time and Groovy are a great fit for each other.
The closest that you can come (that I could find) would be to use the additional Date methods in the Groovy JDK to do this:
int differenceInDays = start - end
which calculates the difference between the two dates in days. This leaves you to convert the days into months yourself, which sucks.
Stick with Joda-Time.
As Jon Skeet mentioned, you are better of using Joda-Time then wrapping your head around this topic.
Be aware though that Joda-Time returns the number of full months between the two dates, including a proper handling of daylight-savings time.