Manipulate time on System.DateTime object - powershell

I have an small (probably) problem in a function that i am writing. The input to the function is two (start & stop) of the type System.DateTime acquired by the function Get-Date. The output is an array of System.DateTime objects (start & stop) that are between the input objects. Ie.
Start Stop
2018-01-14 13:54:15 2018-01-14 13:54:15
2018-01-15 13:54:15 2018-01-15 13:54:15
Works. However in the process of building this array i need to manipulate the time on the start and stop object and to be honest i havent the slightest clue as to how to set the hour, minute and second on an existing object. Desired output is Ie.
Start Stop
2018-01-14 08:00 2018-01-14 17:00
2018-01-15 08:00 2018-01-15 17:00
I've tried using the ParseExact method but it touches the date as well. I can probably send in more than the time to it, but that seems like 'code from hell' ...
PS C:\tmp> (Get-Date).AddDays(-7)
den 10 januari 2018 14:07:13
PS C:\tmp> [datetime](Get-Date).AddDays(-7)::ParseExact("09:00","hh:mm",$null)
den 17 januari 2018 09:00:00
PS C:\tmp>
How on earth can i manipulate the time, and only the time, on an existing System.DateTime object?
As the function itself serves no real value to the problem it has been excluded.

Use the Date portion of the timestamp, then add the desired number of hours (8 for 8AM, 17 for 5PM):
PS> (Get-Date).Date.AddHours(8)
den 17 januari 2018 08:00:00

your question is not very clear is it what you're looking for ?
$date1=[datetime]"2018-01-14 13:54:15"
PS>$date1.AddSeconds(-15)
Sunday, January 14, 2018 1:54:00 PM

Related

Problem with displaying reformatted string into a four-digit year in Stata 17

I turned to a Stata video "Data management: How to create a date variable from a date stored as a string by Chuck Huber" to make sure my date variable were formatted properly, however, I cannot get to show me the reformatted variable (school_year2) to display as a year (e.g. 2018).
Can someone let me know what I may be missing here?
Thank you,
.do file
gen school_year2 = date(school_year,"Y")
format %ty school_year2
list school_year school_year2 in 1/10
+---------------------+
| school~r school~2 |
|---------------------|
1. | 2016 2.0e+04 |
2. | 2016 2.0e+04 |
3. | 2016 2.0e+04 |
4. | 2016 2.0e+04 |
5. | 2016 2.0e+04 |
|---------------------|
6. | 2016 2.0e+04 |
7. | 2016 2.0e+04 |
8. | 2016 2.0e+04 |
9. | 2016 2.0e+04 |
10. | 2016 2.0e+04 |
+---------------------+
.
end of do-file
The value of the underlying data is still days from 1 Jan 1960 as you are using the date() function. So keep %td as you are working with days here, not years. But then you can decide for it to display only the year using %tdCCYY C standing for century and Y for year. But remember, the underlying data point is still the day 1 Jan 2016 and not 2016
clear
input str4 school_year
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
end
gen school_year2 = date(school_year,"Y")
format %tdCCYY school_year2
list school_year school_year2 in 1/10
If year is all you want to work with then use the year() function to get the year from the date. The examples below details steps you can play around with.
clear
input str4 school_year
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
end
gen school_year2 = date(school_year,"Y")
gen school_year3 = year(school_year2)
format %tdCCYY school_year2
format %ty school_year3
list in 1/10
Note that in the last example, all values look the same to you. But the first variable is a string with the text "2016", the second is a date stored as the number of days from 1 Jan 1960 with only its year value displayed, and the last is a number with the number of years from year 0 displayed as a year (which in this case would have been the same had it been displayed as its underlying number).
#TheiceBear has already explained the main point, but here is the story told a little differently in case that is helpful.
The fallacy here is that changing the (display) format is just that, a change in format. It has no effect on what is stored, that is, on the value of data held within variables in the question.
You are using generate to create new variables, which is fine, but the basic principles can be seen directly using di (display) on scalar constants. That's also a good way to check understanding of Stata's rules.
The date() function -- despite its historic name -- is for creating numeric daily dates (only). If you tell date() that your input is a string containing the year only, then it imputes 1 January as day and month. The result is an integer, counted from the origin of the scale at 1 January 1960.
. di date("2016", "Y")
20454
. di date("1 Jan 2016", "DMY")
20454
. di date("1 Jan 1960", "DMY")
0
It is a fair bet that few are willing or able to work out what 20454 is on such a scale, but you can specify a daily date display format so that you and readers of your code can see directly.
. di %td 20454
01jan2016
There are many minor variations on that to display daily dates (or parts of them, such as monthly or yearly dates). The different format names for daily dates all start %td.
Conversely, if you say that the value 20454 is to be displayed using a yearly format, you are referring to the year 20454, several thousand years into the future. Stata doesn't act puzzled, except that it doesn't expect such values as years and just shows you a year rounded to 2.0e+04, that is 20000. If you had good reason to work with dates thousands or millions of years into the future, date display formats are likely to be neither needed nor helpful.
. di %ty 20454
2.0e+04
This paper riffs on the idea that a change in display format is only that and that doesn't affect stored values.

Powershell HowTo get last day of 2 month ago

I've a script scheduled every 4th and 14th day of month.
when script starts on 4th, I need to get the last day of the previous month (easy, $a.AddDays(-5))
when script starts on 14th, I need to get the last day of 2 month before.
for example:
14 april.
I want to get:28 february 2013
how is it possible?
also I want to get the date in format yyyymmdd
UDPDATE:
Is it possible that your solution doesn't work well with the change of the year?
if I'm in january, $(get-date).month -1 results 0. so I did this:
$datenow = Get-date
if $datenow.day -eq 14
$datenow.AddDays(-14)
then I calculate
$LastDayInMonth = [System.DateTime]::DaysInMonth($(Get-date).Year, $(Get-date.Month))
$datenow.AddDays(-$LastDayInMonth)
$datestring = $datenow.ToString("yyyyMMdd")
To get the date in a string:
$(get-date).Date.ToString("yyyyMMdd")
For getting the last day of two months prior:
if($(get-date).Day -eq 14){
$LastDayInMonth = [System.DateTime]::DaysInMonth($(get-date).Year, $($(get-date).Month - 2))
}
Update
The line
$LastDayInMonth = [System.DateTime]::DaysInMonth($(get-date).Year, $($(get-date).Month - 2))
Uses the static method DaysInMonth in the System.DateTime class to get the days in the month of the month that is passed to it. It's documentation is here. It takes as input two parameters, the month as an integer, and the year as an integer as well.
In our case, we want 2 months before this month, so for the month parameter we pass in
$(get-date).month - 2
And that is surrounded in a parenthesis to make sure powershell does the calculation and then pass the result of the calculation. get-date is a powershell cmdlet that gives the current date and time as a .NET dateTime object, so we have all the properties and methods at our disposal.
The whole
[System.DateTime]::DaysInMonth()
is just the way of calling static methods in powershell.
Update 2
Once you get the last day in the month, you can concatenate the string by:
$LastDayInMonthString = "$($(get-date).AddMonths(-2).ToString("yyyyMM"))$LastDayInMonth"
$(Get-Date).addDays(-$(Get-Date).Day).addMonths(-1).ToString("yyyyMMdd")

Date value as float - Apple's iWork Numbers 09 - internal XML

I am writing a script in PHP which will convert Numbers file into HTML table, but I can not figure out which format is used for date storage. The date cell tag looks like
<sf:d
sf:s="SFTCellStyle-128"
sf:w="84.074219"
sf:h="14"
sf:cell-date="371397519.99999952" />
so the date must be in sf:cell-date attribute, but I can not figure out how to convert it into human readable format. Any ideas? I have never seen date value as float number.
As written in a comment, it is the number of seconds since 01/01/2001 at 00:00:00. Equipped with that knowledge and because this goes hand in hand with the UNIX Epoch all you need to do is to define and use the offset. It should be compatible with nearly every of the existing PHP date functions and objects, for example with date:
define('CELL_DATE_EPOCH_OFFSET', 978307200);
$sf_cell_date = 371397519.99999952;
echo date('r', CELL_DATE_EPOCH_OFFSET + $sf_cell_date);
The output of this little script is (in my timezone):
Mon, 08 Oct 2012 15:58:39 +0200
I hope this is helpful. 978307200 is the unix timestamp for 01/01/2001 00:00:00 UTC, you can get with PHP for example with the following code-example:
$base = new DateTime('01/01/2001 00:00:00 UTC');
echo $base->getTimestamp(), "\n";
in case that was your problem.

Server strtotime incorrect

Both strtotime and mktime are outputting an incorrect timestamp and it's driving me mad.
If I add the following strtotime('2012-10-09');
I get 1349701200
Which is actually
Mon, 08 Oct 2012 13:00:00 GMT
I'm using my localhost, running MAMP. I'm assuming it's a server timezone issue, or something, but I don't understand why, or how to fix it.
Any help?
strtotime uses default timezone to interpret the string. If you want different timezone you could specify it explicitly or change it for all calls:
<?php
if (date_default_timezone_get()) {
echo 'date_default_timezone: ' . date_default_timezone_get()."\n";
}
echo strtotime('2012-10-09')."\n"; # default timezone
echo strtotime('2012-10-09 UTC')."\n";
date_default_timezone_set('UTC');
echo strtotime('2012-10-09')."\n";
?>
Output
date_default_timezone: Europe/London
1349737200
1349740800
1349740800
POSIX timestamp counts number of seconds since 1970-01-01 00:00:00 UTC. For example, midnight (00:00) in New York may be 20:00 in UTC at this time of year (the same POSIX timestamp). But 00:00 in UTC and 00:00 in New York correspond to different moments in time (different POSIX timestamps). Local clocks follow the Sun usually (roughly speaking) and even if it is night where you are; the Sun shines somewhere on Earth.

Convert seconds to date from Jan 01 1901 in unix/linux

im trying to convert a time stamp in seconds from Jan 01 1901 to the current date.
for example,
time stamp 3465468225 translate to a date in 2010. does anyone know of a way to do this in unix/linux? thanks.
In R, it is as simple as this:
> as.POSIXct(3465468225, origin="1901-01-01")
[1] "2010-10-25 15:03:45 CDT"
>
This uses appropriate wrappers around C-level calls gmtime() / localtime() plus time formatting via strftime().
On GNU and POSIX systems you can obtain the date string using seconds since Epoch (1970-01-01 00:00:00 UTC) as:
$ date --date=#1289495920
Thu Nov 11 12:18:40 EST 2010
You should handle the offset since Jan 01 1901 yourself.