I am trying to add a day to a date in milliseconds.
The code I am using is bellow.
var x = 1450612800000;
var timeFrame = 'days'
x = moment(x).add(timeFrame, 1);
console.log(x['_i']) //returns 1450612800000
Here is the fix:
var x = 1450612800000;
var timeFrame = 'd'
var newDay = moment(x).add(tf, 1);
console.log('newDay');
//Get New Date in Milliseconds format
console.log(newDay.valueOf());
//Get New Date in Date Format
console.log(newDay.toDate());
Take a look at this StackOverflow answer: https://stackoverflow.com/a/28132227/3692354
_i isn't what you want to use here - that's the input that was used to create the moment object. I think what you want to use instead is moment's valueOf function: http://momentjs.com/docs/#/displaying/unix-offset/
Related
I have a function here that gets the date, and adds one week to it:
func thingy() {
let currentDate = Date()
var dateComponent = DateComponents()
dateComponent.day = 7
let futureDate = Calendar.current.date(byAdding: (dateComponent*i), to: currentDate)
print(futureDate!.formatted())
}
This gets the current date, adds one week to it, and prints out that date.
I want to get a for loop that will give the date, for example maybe 10 weeks in the future, maybe looking something like this:
for i in 1...num[ex: 11] {
let currentDate = Date()
var dateComponent = DateComponents()
dateComponent.day = 7
let futureDate = Calendar.current.date(byAdding: (dateComponent*i), to: currentDate)
let match = (title: "Test", date: futureDate)
}
I get this error:
Referencing operator function '*' on 'DurationProtocol' requires that 'DateComponents' conform to 'DurationProtocol'
How do I fix this?
I would advise adding .weekOfYear to the date. E.g., to get an array of Date representing the next ten weeks:
let calendar = Calendar.current
let today = calendar.startOfDay(for: Date())
let dates = (1 ... 10)
.compactMap { calendar.date(byAdding: .weekOfYear, value: $0, to: today) }
I have to find the difference between two times in 24 hour format. I have the two time strings, Eg: 10:40 and 18:20. How can I find the difference between these two times in Flutter?
You can use intl package.
var format = DateFormat("HH:mm");
var one = format.parse("10:40");
var two = format.parse("18:20");
print("${two.difference(one)}"); // prints 7:40
A complete answer for perfect calculation is given bellow
String start_time = formateTime('12:00'); // or if '24:00'
String end_time = formateTime('24:00'); // or if '12:00
var format = DateFormat("HH:mm");
var start = format.parse(start_time);
var end = format.parse(end_time);
if (start.isAfter(end)) {
print('start is big');
print('difference = ${start.difference(end)}');
} else if(start.isBefore(end)){
print('end is big');
print('difference = ${end.difference(start)}');
}else{
print('difference = ${end.difference(start)}');
}
I am trying to run a 'for' loop that looks like this:
function runMultipleDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
//9/26/2018 is 1569477600000 and 2/28/2019 is 1551337200000; 9/21/2018 is 1537509600000
for (var date1 = 1537596000000 /*9/21/2018*/; date1 < 1569477600000 /*9/26/2018*/; date1 +=86400000) {
//runEverything();
var date2 = new Date();
date2 = date2.setTime(date1);
ss.getSheetByName('Time Range').getRange("A3").setValue(date2);
};
};
My goal is to run a function called "runEverything()" that reference a date located in cell A3 of a sheet called 'Time Range'. As long as the date in cell A3 is less that 9/26/2018, the for loop should run my function 'runEverything' and then set a new date in A3. When I do my test loop, the setValue(date2) returns a numeric value rather than a date in that cell (A3). Can someone point out how I can return the date format rather than the numeric value of date? if there is a more elegant way to achieve this, I am all ears!
Thank you for your insight!
You want to use the date string instead of the unix time
If my understanding is correct, how about the following script? Please think of this as just one of several possible answers.
Sample script:
function runMultipleDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var start = new Date("2018-09-29"); // <--- 1538200800000
var end = new Date("2018-10-05"); // <--- 1538719200000
var step = 1; // 1 day (86400000)
for (var date1 = start; date1 <= end; date1.setDate(date1.getDate() + step)) {
runEverything();
var date2 = new Date(date1.getTime());
date2.setDate(date2.getDate() + 1);
ss.getSheetByName('Time Range').getRange("A3").setValue(date2);
};
}
This script brings the same result with the script of your answer.
When the script is run, 10/06/2018 is put to the cell "A3".
References:
getDate()
setDate()
Tanaike suggested using new Date(date2) in the setValue() and it worked. My final script is something like this:
function runMultipleDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
for (var date1 = 1538200800000 ; date1 <= 1538719200000; date1 +=86400000) {
runEverything();
var date2 = new Date();
date2 = date2.setTime(date1+86400000);
ss.getSheetByName('Time Range').getRange("A3").setValue(new Date(date2));
};
Trying to add 1 day to the simple date format.
import java.text.SimpleDateFormat
Date date = new Date();
def dateformat = new SimpleDateFormat("YYYY-MM-dd")
def currentDate = dateformat.format(date)
log.info "Current Date : " + currentDate
Date date1 = (Date)dateformat.parse(currentDate);
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
log info c1.add(Calendar.Date,1);
Error occurred in line :
"log info c1.add(Calendar.Date,1);"
groovy.lang.MissingPropertyException:No such property: info for class: Script16 error at line: 10
Note : The current date should be any date in future and i want to increment by 1 day.
You can use TimeCategory to add the day as shown below:
use(groovy.time.TimeCategory) {
def tomorrow = new Date() + 1.day
log.info tomorrow.format('yyyy-MM-dd')
}
EDIT: based on OP comments
Here is another away which is to add method dynamically, say nextDay() to Date class.
//Define the date format expected
def dateFormat = 'yyyy-MM-dd'
Date.metaClass.nextDay = {
use(groovy.time.TimeCategory) {
def nDay = delegate + 1.day
nDay.format(dateFormat)
}
}
//For any date
def dateString = '2017-12-14'
def date = Date.parse(dateFormat, dateString)
log.info date.nextDay()
//For current date
def date2 = new Date()
log.info date2.nextDay()
You may quickly the same online demo
Well, the error you provide clearly tells you, that you have a syntax error. It says that there is no property info.
This is because you write
log info c1.add(Calendar.Date,1);
instead of
log.info c1.add(Calendar.Date,1);
If you would have used the correct syntax, it would complain that Calendar has no property Date.
So instead of
c1.add(Calendar.Date, 1)
you meant
c1.add(Calendar.DAY_OF_MONTH, 1)
But in Groovy you can even make it easier, using
c1 = c1.next()
I've this code that changes the maximum date of a second Calendar Extender to 90 days after the date that has been defined in a first one and minimum to the same it has been selected on the first and it works right except for one thing.
var cal2 = $find("calendar2");
var fecha = cal._selectedDate;
var date = fecha.getDate() + 90;
var year = fecha.getFullYear();
var month = fecha.getMonth();
var todayDate = new Date(year, month, date);
cal2._startDate = cal._selectedDate;
cal2._selectedDate = fecha;
cal2._switchMonth(fecha);
cal2._endDate = todayDate;
Problem is that if I first seelct a date on cal, dates are properly shown in cal2, but I select one on cal again then cal2 doesn't display in the same month that cal, what's much worse it displays to select days that would be now impossible to select and in fact you can select them unless you go back first to month mode.
Any idea on how to "refresh" the behavior of the second CalendarExtender?
Thank you.
you can use this method.
Sys.Extended.UI.CalendarBehavior.prototype.refresh = function () {
this._isOpen = true;
this._ensureCalendar();
this.invalidate();
this._isOpen = false;
}
When you add the method only call:
cal2.refresh()