Manipulation of date in moment not working - momentjs

I'm trying to create a new Moment.js object from a specific date and manipulate the dates, but it's not letting me.
In a report, the user has a datepicker and 2 buttons for next week/previous week.
Whatever date is set, when the user clicks 'next week', the datepicker has to be updated and I need to get the start date and end date of that work week. Let's say my datepicker is currently set to Wed 6 Feb 2019. I'm trying to create a function to do 2 things:
- add 1 week to the datepicker ( Wed 13 Feb 2019 )
- get the 2 dates before and after the current work week. (AFTER SUN 10 Feb 2019 and BEFORE MON 18 Feb 2019)
The problem is I can't seem to generate a new date in moment from the datepicker string AND manipulate it.
If I create a new moment without passing in the date, so just moment() all the manipulations work fine.
As soon as I pass a date when creating the new moment(), the manipulations aren't stored. a and b just output as the same date.
What's strange is that I can't seem to store the manipulated date in a variable (a or b), however, my datepicker updates correctly.
var currentDate = $('#datepicker').datepicker('getDate');
var m = moment(currentDate).add(1, 'weeks');
$('#datepicker').datepicker('setDate', m.format('DD-MM-YYYY') );
var a = m.startOf('isoWeek').subtract(1, 'days');
var b = m.endOf('isoWeek').add(1, 'days');
console.log(a.format('YYYY-MM-DD'));
console.log(b.format('YYYY-MM-DD'));
If datepicker is set to Feb 6 2019
Expected result:
Datepicker: Feb 13 2019
a //2019-02-10
b //2019-02-18
Actual result:
Datepicker: Feb 13 2019
a //2019-02-11
b //2019-02-11

Related

moment.js Timezone Parsing and Comparison

I'm receiving timestamps in the following format '2016-08-17T14:00:00-04:00', which I can parse in moment with moment('2016-08-17T14:00:00-04:00', 'YYYY-MM-DDTHH:mm:ssZ').
But the problem is that I want to print out .format('LLLL') and have it read Wednesday, August 17, 2016 10:00 AM, i.e. subtracting -04:00 from 14:00:00 (NY from UTC). It appears that there is a _tzm: -240 property in the moment object that looks like it holds that -4 hours value, but how do I use that property?
The other goal is to be able to pass in the current time and test if it is between the startDate and endDate variables below. I am guessing if I can convert both to NY-EST I can do this, but I can't seem to get moment to accept the timezone parameter.
Any thoughts?
var moment = require('moment');
// Timestamp strings from API
var startDate = '2016-08-17T14:00:00-04:00';
var endDate = '2016-08-17T15:00:00-04:00';
// Create a range from the start and end dates
var range = moment().range(new Date(startDate), new Date(endDate));
// Get the current time
var currentTime = new Date();
// Does the current time fall within the range b/w the start and end dates
range.contains(currentTime);
A solution I found is below. Adding the value from momentObj._tzm to the parsed date.
module.exports.convertDateToProperTimezone = function (dt) {
var _m = moment(dt, 'YYYY-MM-DDTHH:mm:ssZ');
return _m.add(_m._tzm, 'minutes');
};

How to convert 2001 based timestamp to datetime in SQLite?

ZPUBLICATIONDATETIME is of type TIMESTAMP.
So when I do this:
SELECT strftime('%d - %m - %Y ', datetime(ZPUBLICATIONDATETIME, 'unixepoch')) FROM ZTNNEWS;
I get 26 - 05 - 1984 instead of 2015. iOS (Core Data) writes datetime on 1 Jan 2001 based. What is the best approach to get the right date conversion?
Shall I just add 31 years to it or is there an alternative to unixepoch to put in there?
Essentially what I am trying to do is to get the records from past two days:
select *
from ZTNNEWS
where DATETIME(ZPUBLICATIONDATETIME) > DATETIME('now', '-2 day')
But because ZPUBLICATIONDATETIME is of type TIMESTAMP rather than Datetime, it doesn't output anything.
Any advice please?
Just had the same problem. Adding the date value to the seconds of 1 Jan 2001 seems to do the job:
SELECT * FROM ztnnews WHERE DATETIME(zpublicationtime + 978307200) > DATETIME('now', '-2 day');
I used ruby to get the seconds of 1 Jan 2001:
$ irb
2.2.3 :001 > require "time"
=> true
2.2.3 :002 > Time.parse( "2001-01-01T00:00:00Z").to_i
=> 978307200

ASP Classic - Find last weeks Friday or any day using days as numbers (1-7, 1=Monday and so on..)

I would like to find last weeks Friday for example.
Using days as numbers (1 through 7) for example:
1= Monday and so on..
It would be something like this but I'm stuck at the GetLastWeek, Please see below, THANKS.
<%
dim weeknum
weeknum=5
dim GetLastWeek
GetLastWeek=???? <== FIND LAST WEEKS FRIDAY AS A DATE Eg: MM/DD/YYYY
%>
Example: Last weeks Friday was on: <%=GetLastWeek%>
I probably start by working out what is the current day of the week and working back from there, you can use something like this;
Dim today, offsetdays, lastfri
'WeekDay() returns 1 - 7 (Sunday - Saturday).
today = WeekDay(Date())
'Workout the offset then use DateAdd() to minus that number of days.
Select Case today
Case 1 'Sunday
offsetdays = 2
Case 2 'Monday
offsetdays = 3
Case 3 'Tuesday
offsetdays = 4
Case 4 'Wednesday
offsetdays = 5
Case 5 'Thursday
offsetdays = 6
Case 6 'Friday
offsetdays = 7
Case 7 'Saturday
offsetdays = 1
End Select
lastfri = DateAdd("d", -offsetdays, Date())
Bear in mind this is pseudo coded (untested) and could probably be made better by storing the offsets in an array and using that to power the DateAdd() instead.
You can use the Weekday() function to find what day of the week any particular date is. With this you should be able to calculate anything else you like. There is a full reference for the function here:
http://www.w3schools.com/vbscript/func_weekday.asp

Calculate month different using radMonthYearPicker

I have 2 radMonthYearPicker(1 is for start Date another 1 is for end Date)
I want to calculate the month different between this 2 date.
for my start Date, I set my month as dec and year 2012.
for my end Date, I set my month as dec and year 2013
Base on this 2 RadMonthYearPicker, the month apart should be 12 month
The only code I can find out is RadMonthYearPicker.selectedDate
P.S. I was not allow to upload image file due to lack of repulation point
int monthsApart = 12 * (startDate.Year - endDate.Year) +
startDate.Month - endDate.Month;
int result = Math.Abs(monthsApart);

Given a date how to get Sunday & Saturday of that week

I want to get the Sunday & Saturday of the week from which a date is provided.
I have access to the following functions only:
getDate() returns a number from 0-6 (0 being sunday)
getDay() returns a number from 1-31
getMonth() returns a number from 0-11
getFullYear() returns the current year
I am doing this on titanium.
Per your description above, I came up with:
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDate() + getDay());
var sun = new Date(input.getFullYear(), input.getMonth(), getDay() + (input.getDate() - 6));
If I follow the MDN doc, I come up with (works in Ti too):
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDay() + input.getDate());
var sun = new Date(input.getFullYear(), input.getMonth(), input.getDate() + (input.getDay() - 6));
Where input is a javascript Date object.
The date object will take care or changing the month and/or year if necessary.
Hope this helps.

Resources