# F\*\*\*\*\*\* timestamps/dates

**URL:** <https://forum.inductiveautomation.com/t/f-timestamps-dates/2593>\
**Category:** Ignition\
**Created:** [February 1, 2011, 6:21am UTC](https://forum.inductiveautomation.com/t/f-timestamps-dates/2593 "2011-02-01T06:21:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://forum.inductiveautomation.com/u/Robert)\
**Post date:** [February 1, 2011, 6:21am UTC](https://forum.inductiveautomation.com/t/f-timestamps-dates/2593/1 "2011-02-01T06:21:50Z")

</div>

How can I convert the Date datatype in to seconds since epoch?

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [February 1, 2011, 7:20am UTC](https://forum.inductiveautomation.com/t/f-timestamps-dates/2593/2 "2011-02-01T07:20:22Z")

</div>

[unix\_timestamp()](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp) in MySQL. It varies from platform to platform. What DB are you using?

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [February 1, 2011, 5:14pm UTC](https://forum.inductiveautomation.com/t/f-timestamps-dates/2593/3 "2011-02-01T17:14:43Z")

</div>

If you have a Date in scripting, this will convert it to seconds since epoch (Java epoch is Jan 1, 1970)

`date = ...
secondsSinceEpoch = data.getTime() / 1000`

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://forum.inductiveautomation.com/u/Robert)\
**Post date:** [February 1, 2011, 6:05pm UTC](https://forum.inductiveautomation.com/t/f-timestamps-dates/2593/4 "2011-02-01T18:05:20Z")

</div>

Thanks Carl,

I have a date from a date picker component. I needed to get it into epoch seconds to use in a database query to compare against a timestamp. (t\_stamp column in sqlt\_data\_xxx table to be precise)

Nathan, I’m using PostGres.

The solution I worked out was to use:

```auto
select extract(epoch from timestamp with time zone '"+system.db.dateFormat(date, 'yyyy-MM-dd HH:mm:ss.SSSZ')+"')
```

I like Carl’s solution better. Why the ‘/1000’? does getTime() return milliseconds since epoch?

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [February 1, 2011, 6:06pm UTC](https://forum.inductiveautomation.com/t/f-timestamps-dates/2593/5 "2011-02-01T18:06:21Z")

</div>

yep - milliseconds. See the [javadocs](http://download.oracle.com/javase/6/docs/api/java/util/Date.html#getTime()) for details.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [February 15, 2023, 2:41pm UTC](https://forum.inductiveautomation.com/t/f-timestamps-dates/2593/6 "2023-02-15T14:41:13Z")

</div>

I am trying to make sure my notes are right.

**Datetime expression info:**

Today 7am - addHours(midnight(now(0)), 7)  
Yesterday 7am - addDays(addHours(midnight(now(0)), 7), -1)  
2022-04-27 07:00:00

**Datetime excel**

=TEXT(TIMEVALUE(T1)-TIMEVALUE(S1),"hh:mm:ss")

**Datetime Script info:**

```auto
# Getting epoch time using parse
test = "2023-02-15 14:30:00"
epochTime = system.date.toMillis(system.date.parse(test, "yyyy-MM-dd HH:mm:ss"))
print(epochTime)

# Getting epoch time using setTime
now = system.date.now()
yesterday = system.date.addDays(now, -1)
passParamWhen1stShift = system.date.setTime(yesterday, 14,59,0)
print (passParamWhen1stShift)
epochTime = system.date.toMillis(passParamWhen1stShift)
print(epochTime)

```

**Datetime SQL info:**

DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)  
gets the start of the month, can be used to truncate to hour  
DATEADD(Hour, DATEDIFF(Hour, 0, GETDATE()), 0)

select DATEDIFF(MONTH, 0, GETDATE()) will give the number of months from 1900-01-01  
When 2012-07-01 it is 1350  
select DATEADD(MONTH,1350,0) will give 2012-07-01 00:00:00.000  
which is the start of that month.

Cast(getdate() as Date) will eliminate the time portion 2022-04-27 00:00:00 in SQL

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 15, 2023, 3:49pm UTC](https://forum.inductiveautomation.com/t/f-timestamps-dates/2593/7 "2023-02-15T15:49:59Z")

</div>

> [@zacharyw.larson](#):
>
> I am trying to make sure my notes are right.

Maybe run some tests against your code? Instead of playing necromancer with a 12 year old post?

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [February 15, 2023, 4:04pm UTC](https://forum.inductiveautomation.com/t/f-timestamps-dates/2593/8 "2023-02-15T16:04:23Z")

</div>

It is the post that came up when I searched.  
I want to find the information when I search.

Functionality isn't the concern, but technique.  
I want to use good techniques.
