I need to test an agenda and I need getting the actual date and current hour to assert with the agenda values.
This not works:
var d = new Date();
expect(day.getText()).toEqual(d.getDate());
You can get current hour using JavaScript inbuilt getHours() method. Here's its usage -
var cTime = Date().getHours();
or
var d = new Date();
var cTime = d.getHours(); //prints the current hour only
And you can get the actual date using getDate() method which prints a string of values related to current date and time. Sample format: Fri Nov 20 2015 20:24:38 GMT+0530 (IST). Hope it helps
Related
This question already has answers here:
Calculate days to go until a particular date with momentjs
(3 answers)
Closed 1 year ago.
I want to validate if the following date is valid : 30 Feb 2021.
So the main issue is 30 Feb 2021 shouldn't be a valid date or 31 April 2021. is it possible to achieve that with moment js or luxon? or should i use another approach?
let m = moment([2021, 2, 31]);
console.log("Date is valid", m.isValid()); \\returns true which is not a valid date!
I don't know moment.js or luxon , but it's achievable in basic JS (although a little long-winded)
JavaScript will up-tick the date, so the 29th of Feb is the 1st of March.
Therefore we can compare the before and after version :
var month = 1; // zero based - 1 is feb
var day = 30;
var year = 2021;
var testDate = new Date(year, month, day); // will convert to 2nd of March
var parsedDate = testDate.getDate(); // will be 2
var parsedMonth = testdate.getMonth() // will be 2 , zero based, March
var parsedYear = testDate.getFullYear(); // remains 2021 in this case
var isValidDate = parsedDate === day && parsedMonth === month && parsedYear === year;
This could be condensed a lot but I've made it as bloated as possible for readability
you could easily turn it into a function, something like this (untested)
function IsValidDate(year, month, day){
var InputMonthsAreOneBased = 0; // change to 1 if needed
var testDate = new Date(year, month - InputMonthsAreOneBased, day );
return testDate.getDate() === day && testdate.getMonth() === (month - InputMonthsAreOneBased) && testDate.getFullYear() === year;
}
Hi I need help with this:
I am working on a macro for which I need to get the value of a cell that has the TODAY() formula in it. My spreadsheet is displaying CET accordingly to my location.
When I do:
var today = sheet.getRange('B1').getValues().flat();
Logger.log(today);
I get:
[Wed Feb 24 18:00:00 GMT-05:00 2021]
On the spreadsheet I see 25.02.2021 11:36 (CET), and in Apps Script, it says current EST should be 25.02.2021 05:36. The date is completely off.
Entire code:
function PrepareColumn() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('TheSheetName');
var dateRange = sheet.getRange(2, 1, 1, 15).getValues().flat();
var today = sheet.getRange('B1').getValues().flat();
Logger.log(today);
}
Two modification points:
Use getDisplayValue to get the value that is displayed in the sheet.
For today you are fetching a single cell, you don't need getValues and then flat.
Replace:
var today = sheet.getRange('B1').getValues().flat();
to:
var today = sheet.getRange('B1').getDisplayValue();
and maybe you want to do the same for dateRange:
var dateRange = sheet.getRange(2, 1, 1, 15).getDisplayValues().flat();
I'm trying to build my first app using flutter framework. The app is about my "End of Year Challenge". It started from 1st Sept 2019 and will last till the end of this year.
What I'm trying to achieve is - I want to display the current day number of the challenge period. eg: 1st Sept is Day 1, 30th Sept is Day 30 and 1st Oct is Day 31 and so on.
I'm trying to get the first day of Sept and assign it to 1. Then using a loop I want the app to update the day to the current day. The loop will stop once the current day equals to 122 (as this would be the last day of the challenge)
Here's the screenshot of the UI
final firstSeptember = DateTime.utc(2019, DateTime.september, 1);
static const totalNumberOfDays = 122;
int noOfDay(){
int dayOne = firstSeptember.day; // I'm just trying codes, IDK the actual code/business logic
return dayOne;
}
In function you'd use
int noOfDay(){
var todayDate = DateTime.now();
final firstSeptember = DateTime.utc(2019, DateTime.september, 1);
var difference = todayDate.difference(firstSeptember);
return difference.inDays + 1;
}
Explanation:
Get today's date
var todayDate = DateTime.now();
You already have start date which is
final firstSeptember = DateTime.utc(2019, DateTime.september, 1);
All you need to do is subtraction.
var difference = todayDate.difference(firstSeptember);
int daysCompleted = difference.inDays + 1;
The example code below gets DateComponents from the current Date, modifies the components, and creates a new Date from the modified components. It also shows creating a new DateComponents object, filling it out, and then creating a new Date from that.
import Foundation
let utcHourOffset = -7.0
let tz = TimeZone(secondsFromGMT: Int(utcHourOffset*60.0*60.0))!
let calendar = Calendar(identifier: .gregorian)
var now = calendar.dateComponents(in: tz, from: Date())
// Get and display current date
print("\nCurrent Date:")
print("\(now.month!)/\(now.day!)/\(now.year!) \(now.hour!):\(now.minute!):\(now.second!) \(now.timeZone!)")
let curDate = calendar.date(from: now)
print("\(curDate!)")
// Modify and display current date
now.year = 2010
now.month = 2
now.day = 24
now.minute = 0
print("\nModified Date:")
print("\(now.month!)/\(now.day!)/\(now.year!) \(now.hour!):\(now.minute!):\(now.second!) \(now.timeZone!)")
let modDate = calendar.date(from: now)
print("\(modDate!)")
// Create completely new date
var dc = DateComponents()
dc.year = 2014
dc.month = 12
dc.day = 25
dc.hour = 10
dc.minute = 12
dc.second = 34
print("\nNew Date:")
print("\(dc.month!)/\(dc.day!)/\(dc.year!) \(dc.hour!):\(dc.minute!):\(dc.second!) \(now.timeZone!)")
let newDate = calendar.date(from: dc)
print("\(newDate!)")
In the case where I modify the components, setting different year, month, day, etc., then use the components to get a date, I get the unexpected result that the new date has all the modified components except the year, which remains unchanged.
In the case where I create a DateComponents object and fill it out then create a Date from it, it works as expected.
The output of the code is shown below:
Current Date:
3/9/2017 19:5:30 GMT-0700 (fixed)
2017-03-10 02:05:30 +0000
Modified Date:
2/24/2010 19:0:30 GMT-0700 (fixed)
2017-02-25 02:00:30 +0000
New Date:
12/25/2014 10:12:34 GMT-0700 (fixed)
2014-12-25 17:12:34 +0000
I expected the modified Date to be 2010-02-25 02:00:30 +0000 rather than 2017-02-25 02:00:30 +0000. Why isn't it? Why does it work in the second case?
The docs for DateComponents say: "An instance of NSDateComponents is not responsible for answering questions about a date beyond the information with which it was initialized...". Being that the DateComponents object was initialized with a year, it doesn't seem like this would apply, but it's the only thing I saw in the docs that might explain the behavior I observe.
If you log now and dc you will see the problem. now is being created from a Date. This fills in all of the date components including yearForWeekOfYear and several of the weekday related components. These components are causing modDate to come out incorrectly.
newDate works as expected because only the specific components are being set.
You can get modDate to come out correctly if you reset some of the extra components. Specifically, adding:
now.yearForWeekOfYear = nil
just before creating modDate will result in the expected date for modDate. Of course the best solution is to create a new instance of DateComponents and use specific values from the previous DateComponents as needed:
let mod = DateComponents()
mod.timeZone = now.timeZone
mod.year = 2010
mod.month = 2
mod.day = 24
mod.hour = now.hour
mod.minute = 0
mod.second = now.second
print("\nModified Date:")
print("\(mod.month!)/\(mod.day!)/\(mod.year!) \(mod.hour!):\(mod.minute!):\(mod.second!) \(mod.timeZone!)")
let modDate = calendar.date(from: mod)
print("\(modDate!)")
I have a date in string format as follows-
"Fri Jul 11 2003 19:05:44 GMT+0530 (India Standard Time)"
I want to check whether the date is a month old or not.
For this I am doing like this-
var todaysDate= new Date();
todaysDate.setDate(todaysDate.getDate() - 30);
if(Ext.util.Format.dateRenderer("Fri Jul 11 2003 19:05:44 GMT+0530 (India Standard Time)", "D M d Y g:i:s") <= todaysDate)
{
}
It should return true but it is returning false. What I am doing wrong here.
Please help.
You're comparing a renderer to a date, dateRenderer doesn't return a date, it returns a renderer. Renderers are used for example for grid cells, when you have a grid that has a record with a date and you want to display that date in a particular format you use a renderer to tell the grid cell how to format that date.
Check the docs about this: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.util.Format-method-dateRenderer
Also I believe dateRenderer takes only one argument, you're passing two.
You can use regular Javascript for this:
var oldDate = new Date("Fri Jul 11 2003 19:05:44 GMT+0530 (India Standard Time)");
var newDate = new Date()
newDate.setDate(newDate.getDate() - 30);
if(oldDate <= newDate){
doSomething()
}