I want convert milliseconds to date time follow the format mm/dd/yyyy in powershell, my code is below:
$data+= $dataList.Rows[$i]["ROOM_LASTVISITDATE"].ToString() + "`t"
The result of 1278504562790. So, how i can convert it to date time in powershell, please help me. Thanks
To convert a epoch/unix timestamp to a human readable date with Powershell, you can use the DateTimeOffset type.
[datetimeoffset]::FromUnixTimeMilliseconds(1278504562790).DateTime
Your code could then look like this
$lastVisited = $dataList.Rows[$i]["ROOM_LASTVISITDATE"].ToString()
$data += [datetimeoffset]::FromUnixTimeMilliseconds($lastVisited) + "`t"
Assuming the offset is the start of the UNIX epoch (01/01/1970), you could simply add the milliseconds to that offset:
$EpochStart = Get-Date 1970-01-01T00:00:00
$myDateTime = $EpochStart.AddMilliseconds(1278504562790)
Related
Can anyone help me with the following:
I have am recording date in my UI, which is IST +5:30
First, I want to convert that date to UTC with start time 00:00
Second, I want to convert that to long time (which I think it is
Unix)
Saved to DB
Third, I want to convert a long time back to UTC in format
MM/DD/YYYY.
This is what I tried so far:
const dateUnix => moment(myMomentObj)
.utc()
.format(DATE_TIME_FORMATS.TIME_STAMP);
The above gets a long time which I don't know if it correct.
const dateMoment = moment.unix(dateUnix)
const formatedDate = dateUnix.format('L'); //which should be in MM/DD/YYYY format
But the formatDate is giving me something like 02/12/15235 which is wrong.
Any help is appreciated.
Thanks in advance.
This code might help you
//input in IST +5:30
var inputDate = moment().utcOffset("+05:30").format();
console.log(inputDate);
//moment.unix outputs a Unix timestamp
var unixTs = moment.utc(inputDate).unix();
console.log(unixTs);
//there is a unix method that accepts unix timestamps in seconds followed by format to format it
var formattedDate = moment.unix(unixTs).format("MM/DD/YYYY");
console.log(formattedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Im trying to convert the first two columns of a cell into a Matlab time. First column {1,1} is the date in YYYY-MM-DD format and the second is the time in HH:MM format.
Any ideas where I'm going wrong? My code:
file = 'D:\Beach Erosion and Recovery\Bournemouth\Bournemouth Tidal
Data\tidal_data_jtide.txt'
fileID = fopen(file);
LT_celldata = textscan(fileID,'%D%D%D%D%d%[^\n\r]','delimiter',',');
formattime = 'yyyy-mm-dd HH:MM'
date = LT_celldata{1,1};
time = LT_celldata{1,2};
date_time = datenum('date','time'); code
Screenshot below is LT_celldata{1,1} :
You can combine variables date and time with the following code:
date = datetime(LT_celldata{1,1},'InputFormat','yyyy-MM-dd');
time = datetime(LT_celldata{1,2},'InputFormat','HH:mm:ss','Format','HH:mm:ss');
myDatetime = datetime(date + timeofday(time),'Format','yyyy-MM-dd HH:mm:ss');
The code uses timeofday function to combine date and time information from the two different variables. You may find more information and examples at this documentation page.
Is it possible to retrieve only the date portion of a datetime object in PowerShell? Reason being I need to compare the LastWriteTime property of files with today's date to determine whether to backup a file or not. As-is a datetime object includes the time as well which will always evaluate to false when I do something like:
if ($fileDate -eq $currentDate) {
# Do backup
}
I haven't found anyway to do this. If we use the format operator or a method, it converts the object to a string object. If you try to convert that back to a datetime object, it appends the time back onto the object. Probably something simple, but I've been looking at this script for a while and that's the last part that's breaking.
EDIT: As #jessehouwing points out in the comments below, my answers are unnecessarily complicated. Just use $datetime.Date.
A couple of ways to get a DateTime without any time component (ie set to midnight at the start of the date in question, 00:00:00):
$dateTime = <some DateTime>
$dateWithoutTime = $dateTime.AddSeconds(-$dateTime.Second).AddMinutes(-$dateTime.Minute).AddHours(-$dateTime.Hour)
or
$dateTime = <some DateTime>
$dateWithoutTime = Get-Date -Date ($dateTime.ToString("yyyy-MM-dd"))
I ran each version in a loop, iterating 100,000 times. The first version took 16.4 seconds, the second version took 26.5 seconds. So I would go with the first version, although it looks a little more complicated.
Based on answers found here: https://techibee.com/powershell/powershell-how-to-query-date-time-without-seconds/2737 (that article is about stripping just the seconds from a DateTime. But it can be extended to stripping hours, minutes and seconds).
Assuming $fileDate is not a dateTime object, then you can just convert both to strings and format them.
if ($fileDate.ToString() -eq $currentDate.ToString("dd/MM/yyyy")) {
# Do backup
}
This will not answer how to remove time on datetime, but to do your validation purpose of identifying when to backup.
I do suggest to subtract your two given date values and compare the result if total hours are already met to do your backup.
if (( $currentDate - $fileDate ).TotalDays > 7) {
# Do Backup
}
you can also validate for the following
Days :
Hours :
Minutes :
Seconds :
Milliseconds :
Ticks :
TotalDays :
TotalHours :
TotalMinutes :
TotalSeconds :
TotalMilliseconds :
Assuming $currentTime contains a DateTime object, you can retrieve a new DateTime object with the same date but with the time portion zeroed like this:
$midnight = Get-Date $currentTime -Hour 0 -Minute 0 -Second 0 -Millisecond 0
I am looking for total mins for today,
Tried this way but not working.
$from_date = Get-Date
$Start_date = $from_date.ToShortDateString()
($from_date - $Start_date).Minute
I'd do it this way:
PS C:\> (Get-Date).TimeOfDay.TotalMinutes
651.356536988333
Don't use ToShortDateString() if you want to do further arithmetic cprocessing on the object, it'll turn it into a string.
Substract the Date property from the object and grab the TotalMinutes property value from the resulting timespan:
$from_date = Get-Date
$MinutesSinceMidnight = ($from_date - $from_date.Date).TotalMinutes
The result will be in decimal form. Use Math.Floor() if you need an integer value:
[System.Math]::Floor($MinutesSinceMidnight)
I'm having trouble finding a good way of formatting a UTC-time stamp with this format: yyyyMMdd-HH:mm:ss.<three additional digits>
I wasn't able to find any character that represents milliseconds/hundredths, I'm not even sure this is possible, to parse that format that is.
Ideally I'd like to use the parseToStringDate that's part of the Date library.
My plan b is to convert yyyyMMdd-HH:mm:ss to milliseconds and then add the three last digits to that number.
Use yyyyMMdd-HH:mm:ss.SSS
This will get you milliseconds as well.
Test Code:
def now = new Date()
println now.format("yyyyMMdd-HH:mm:ss.SSS", TimeZone.getTimeZone('UTC'))
I would convert it like that:
def now = new Date()
println now.format("YYYYMMdd-HH:mm:ss")
You can try this:
TimeZone.getTimeZone('UTC')
Date date = new Date()
String newdate = date.format("YYYY-MM-dd HH:mm:ss.Ms")
log.info newdate