Add hours to timestamp in moment.js - momentjs

I have two inputs first input is for getting the date and the second input is for getting the time of the day.
For example,
User selects date: 20 January 2019 and time: 12:30 pm.
When I convert the date input to Unix timestamp I get the timestamp for 00:00 hours on 20 January 2019.
startingDate = moment(date.startsOn).unix();
So I want to add hours that I'm getting from the user into the date timestamp.
When I try to convert hours into Unix timestamp I get a timestamp for the present day along with the hours entered by the user i.e. I get timestamp for 12:00 pm on 11 January 2019.
startingTime = moment(date.startingTime).unix();
Any suggestion how I can achieve this?

I managed to do this in this way:
As I had my starting date and time in unix format
startingDate = moment(date.startsOn).unix();
startingTime = moment(date.startingTime).unix();
I had to convert them into moment format
const date = moment.unix(this.startingDate).format('YYYY-MM-DD h:mm a');
const hours = moment.unix(this.startingTime).format('HH');
Then I added hours to my date constant
var finalTime = date.add(hours, 'hours');
Then I posted this finalTime to my API.

Related

What date format is "623548800"?

I exported the SQLite db from an iOS app and was wanting to run a query based on the date, but I found that it's in a format I don't recognize. As stated above, the latest value is "623548800". I'm assuming this corresponds to today, since I created a record in the app today. This is 9 digits, so it's too short to be a Unix timestamp, which is 10 digits.
The earliest record in the db is "603244800", which likely corresponds to when I started using the app on 2/13/2020. That's a difference of 20,304,000, so it looks like it's using seconds, as it's been 20,312,837 seconds since then.
Is this essentially tracking seconds based on some proprietary date, or is this a known format?
623548800 - 603244800 = 20304000
20304000/86400 seconds in 24 hours = 235 days
October 5, 2020 - February 13, 2020 = 235 days
UTC Unix timestamp February 13, 2020 = 1581552000
Like the prior comment said it looks like an offset, it might be a timestamp somewhere in source or in db
Your dates are Unix Timestamps.
By using any on line converter (like https://www.epochconverter.com) you can find the dates they correspond to.
The latest value 623548800 corresponds to Thursday, October 5, 1989 12:00:00 AM GMT
and the earliest value 603244800 corresponds to Sunday, February 12, 1989 12:00:00 AM GMT.
So it seems like your dates or off by 31 years.
I found a similar case here: Behind The Scenes: Core Data dates stored with 31 year offset?
If you want you can convert them to the format 'YYYY-MM-DD hh:mm:ss' like this:
UPDATE tablename
SET datecolumn = datetime(datecolumn, 'unixepoch', '+31 year')
or:
UPDATE tablename
SET datecolumn = date(datecolumn, 'unixepoch', '+31 year')
if you are not interested in the time part.

Is there a Stata function to delete the date in a DateTime variable?

I have a date-time variable called assign1_submission (type double and format %tchh:MM:SS_AM). I have another date-time variable time_due (type double and format %tchh:MM:SS_AM).
gen double time_due = clock("12:00 PM", "hm")
format time_due %tchh:MM:SS_AM
In the assign1_submission that I imported from MS Excel, the variable only contained a time. However, in the data browser, Stata attached a date to the entry.
Despite only telling Stata a time, my time_due variable has a date in the entry.
What I want to do is delete the date in both variables, because I want to see the time difference between time_due and assign1_submission.
So, I tried the following code:
replace assign1_submission = hh(assign1_submission)
replace time_due = hh(time_due)
gen time_difference = time_due - assign1_submission
According to Stata:
hh(e_tc)
Description: the hour corresponding to datetime e_tc (ms. since
01jan1960 00:00:00.000)
When running this code:
replace assign1_submission = hh(assign1_submission)
Stata disregards the previous time: everything is overwritten to state: 12:00 AM, where a date is still attached to the entry.
Can someone please help to delete the date in both the assign1_submission and time_due variables so that I can determine the difference in hours between the two variables?
Date-times in Stata are counted in milliseconds (ms) since the start of 1960. You can discard the date element by getting the remainder on division by 24 * 60 * 60000, the number of ms in 1 day.
The time concerned is still tacitly a time on 1 January 1960.
clear
set obs 1
gen double datetime = clock("4 July 2020 18:00:00", "DMY hms")
format datetime %tc
gen double time = mod(datetime, 24 * 60 * 60 * 1000)
format time %tcHH:MM:SS
l, noobs
+-------------------------------+
| datetime time |
|-------------------------------|
| 04jul2020 18:00:00 18:00:00 |
+-------------------------------+
Otherwise it's hard to follow your assertions. Stata doesn't change data because you look in the browser. What you are showing as daily dates pertains to different variables.

Determine hours using date and time fields

I have separate date and time fields for an event and the data entry of that event. How do I determine the hours between the event and the data entry?
The time field is a string in standard time which I converted to a military time time field using a formula.
date is written in mm/dd/yyyy format and time is in standard time hh:mm am/pm which I've converted to HH:MM military time.
If an event happened at 12:10 pm on 12/3/2016 but it was entered at 1:25 pm on 12/5/2016, I want to see 49 Hours and 15 Minutes in my final field
DateDiff is just what you need. Something like:
DateDiff("h", DateTime({StartDate},{StartTime}), DateTime({EndDate},{EndTime}))
& " Hours and " &
DateDiff("n", DateTime({StartDate},{StartTime}), DateTime({EndDate},{EndTime})) Mod 60
& " Minutes"
This will retrieve the difference between your two dateTimes twice - Once in hours, once in minutes. Perform Mod 60 on the minutes section to get only the remainder minutes after subtracting 60 minutes for each hour.

Solr: Day of week, second of day with timezones

We require to search by day of week and time of day using SOLR.
That`s why we first convert date to UTC and then create two index fields: UTC second of day and UTC day of week.
We know what timezone user requests, so we are able to shift this required values and create query:
Monday from 4:00 to 6:00 AM in +5 timezone
is converted to search values for UTC:
(Sunday from 23:00 to 24:00) OR Monday (0:00 to 1:00)
Unfortunatelly this query will fail in timezones with daylight saving time, when timezone is shifted +5 is +6.
Is there a way how to query by correct time of day?
Only solution we could think of is add to query also date ranges, which were in summer/winter time:
((Sunday from 23:00 to 24:00) OR Monday (0:00 to 1:00)) AND (30.10.2015 TO 27.3.2016) OR Monday (0:00 to 2:00)) AND (27.3.2015 TO 30.10.2015 ) OR ... OR ... ,
where these ... represents date, when was switched from summer to winter time.
Data may be recorded in any timezone and viewed in any timezone as well

Calendar not displaying all stored dates

I am using an ASP calendar to show a list of dates, however the way the calendar overlaps and may show the last few dates of March, with the entire month of April, with the first few days of may(please see images below) is causing a problem.
I have set the 'startDate' and 'endDate' to be the start and end of that month. So if the user clicks 10th April, it will show all the stored dates btn 1st april to the 30th april. I need to change this to include the month b4 and the month after, SO any date in april will include all of March, April and May.
'Green is the date selected by the user, red is todays date, and blue are the stored dates in the DB table.'
DateTime startOfMonth = new DateTime(DiaryDate.Year, DiaryDate.Month, 1);
DateTime endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);
The above code selects the first and last date of each month (of the chosen date)
I want to select the entire previous current and next month
not sure on the correct syntax. any help appreciated?
Try this:
// First, get the dates a month before - and after - the specified date.
DateTime nextMonth = DiaryDate.AddMonths(1);
DateTime lastMonth = DiaryDate.AddMonths(-1);
// Get the last day of next month.
DateTime endOfNextMonth = new DateTime(nextMonth.Year, nextMonth.Month,
DateTime.DaysInMonth(nextMonth.Year, nextMonth.Month));
// Get the first day of last month.
DateTime startOfLastMonth = new DateTime(lastMonth.Year, lastMonth.Month, 1);
Now you can simply use endOfNextMonth and startOfLastMonth as your "boundary" dates, as you are currently doing with startOfMonth and endOfMonth.

Resources