Get Day, Month, and Year from Time in Go Language - date

I have an object like this:
type searchObj struct {
symbol string
dataType string
fromDate time.Time
toDate time.Time
}
I want to be able to parse out the day, month, and year from the fromDate and the toDate. How can I do this? Is there a better type to use like (Date) because I do not need the time piece of it?
so I want to be able to pass a date like this 02/19/2016 and be able to get data.Day = 19, date.Month = 02, date.Year = 2016.
I was trying something like this:
search.fromDate.Date.Month
search.fromDate.Date.Day
search.fromDate.Date.Year
This is an example of what I am currently using to create the searchObj:
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
I am new to Go and thank you for the help!

All the methods you're looking for exist on the time.Time type. You can just do;
year := fromDate.Year()
month := fromDate.Month()
day := fromDate.Day()
EDIT:
I suppose it would be more concise to use time.Date like so;
year, month, day := fromDate.Date()

The time type also has Date() method which returns the year, month and day of the time in single call.

With the accepted method, if you need the month number you can simply cast the Month object to int since it's an enum:
year, month, day := time.Now().Date()
log.Printf("%v-%v-%v", year, int(month), day)

Related

Create agent and give its population different id/names and stop each id delay on different time

I have an agent and set its parameter Name and give 3 different names
What I want to do is stop the delay of each name on a different calendar date. How I can achieve that?
You can get the current year, month and day with the following functions:
int getYear(Date date) — returns the year of the current date.
int getMonth(Date date) — returns the month of the current date: one of the constants JANUARY, FEBRUARY, etc.
int getDayOfMonth(Date date)— returns the day of the month of the current date: 1, 2, …
Based on these, you can get the first agent in the delay block with delay.get(0) and use if function.
Pseudo-code like this;
if (date==x){
if (delay.get(0)==myAgents.get(0)){
stopDelay(delay.get(0));
}
}
or
if (date==x){
if (delay.get(0).Name=="yyy"){
stopDelay(delay.get(0));
}
}

Convert year and month variables to timestamp in Postgresql

I have to variables: year and month, both of type integer.
E.g:
year = 2016
month = 1
I want to, in my select statement, return a timestamp given those two variables.
I've had a look at the documentation, specifically to_timestamp and to_date, but all the examples I've come across show a string being converted into a timestamp.
I do not really want to convert my year and month into a string, such as:
to_timestamp(to_char(int,year) + ' ' + to_char(int, month),YYYY/MM/DD HH24:MI:SS)
So, how can I (if it is possible) convert my year and month into a timestamp?
If you are using Postgres 9.4 or later, you could try using make_timestamp():
make_timestamp(2016, 1, 1, 0, 0, 0.0)
This would create a timestamp for January 1, 2016 at midnight. We need to specify values for the other components, even if they end up not being relevant to your query/calculation (e.g. you only need the date).

How do I Determine The Number Of Days In A Month from a particular date in angularjs 2?

I am using angularjs 2 and type script.
I want the number of days in a month from a particular date given by user.
How do I Determine that?
Please help. Thanks in Advance.
As pointed out by Evgeny Shmanev, this answer is incorrect. Please see the correct answer by Zvi Tarem
You don't need angular, you can call this function:
function daysRemainingInMonth(date) {
var year = date.getYear();
var month = date.getMonth();
return new Date(year, month, 0).getDate()
}
Edit: Updated now that I know you have the date
It may be a bit late, but the right answer is a follows:
In order to find the number of days in a month, you need to get the 'date' of the 0th day of the next month:
let daysInMonth = new Date(year, month + 1, 0).getDate();

How to create a specific date in Google Script

I have a spreadsheet that asks people to enter in a day of the month when we need to send out a bill. What I want to do is create a calendar event based on that. So, essentially what I need is an event that starts at the current month, day from the spreadsheet, and continues to a specified point in time.
var monthlyDate = row[6]; // Seventh column, monthly date of payment
var curDate = new Date();
var curMonth = curDate.getMonth();
var curYear = curDate.getYear();
curDate.setDate(curMonth, monthlyDate, curYear);
Logger.log("Day of month: %s", monthlyDate);
Logger.log("Current Date: %s", curDate);
Logger.log("Current Date: %s", Date());
What I'm seeing is that the monthly date is coming in as a float "6.0" for example, and no matter what I enter in for monthlyDate in the setDate line, it keeps setting the date to 10/9/15 (Today is 10/15/15). I've hard-coded that value to many different numbers, but for some reason it's just not working.
How can I create a date (in any format) that follows the scheme "Current Month / Day from Speadsheet / Current Year" ?
The getMonth() method returns a "zero-indexed" number. So, it returns the number 9 for the 10th month. setDate() doesn't set the date, it sets the "Day of the Month". The name of that method is misleading.
Documentation - setDate()
So, the last two parameters that you are using in setDate() are doing nothing. You are setting the day of the month to 9.
If you want to set multiple date parameters at the same time, you need to use the new Date() method:
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
The month parameter accept values from 0 to 11, 0 is Jan and 11 is Dec
Date Reference

Groovy get today's date at minute 00:00:00

I have this problem. I need to do the following:
get todays date
make a new date which will be today's date at 00:00:00
make another date which will be today's date at 23:59:59
For example. Today Date is 12-January-2012 19:00
How can i make a new date, which will be 12-January-2012 00:00 (the start of the current day)
It may seems easy, but i couldnt find any groovyway to get it, any help would be apreciated.
To get the date at midnight use Date.clearTime (docs):
dateAtMidnight = new Date()
dateAtMidnight.clearTime()
(Javadocs are for Groovy JDK < 2.0, clearTime() is declared void in Groovy JDK 2.0, preventing d = new Date().clearTime(). Comments indicate the original functionality may be restored, yay!)
For the comparison, instead of using <= 23:59:59, use < (the next day):
(aDate >= dateAtMidnight) && (aDate < (dateAtMidnight + 1))
An alternative way, but it sets the datetime (but it doesn't get the date merely)
dateAtMidnight = new Date()
dateAtMidnight.set(hourOfDay: 0, minute: 0, second: 0)