# How can I take start of currentdate

**URL:** <https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270>\
**Category:** Help Hub\
**Tags:** variables, json\
**Created:** [November 15, 2024, 10:05am UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270 "2024-11-15T10:05:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![technical-geoscien17](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@technical-geoscien17](https://community.postman.com/u/technical-geoscien17)\
**Post date:** [November 15, 2024, 10:05am UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270/1 "2024-11-15T10:05:52Z")

</div>

I want to display only currentdate’s data.  
And i want to compare it against a field which is a timestamp.  
So i need to take start of current date i.e current date at 00:00:00 hrs sec  
For eg. if current date is 15 Nov 2024. I want the output to be -  
2024-11-15 00:00:00

I can’t use timestamp below code here, as it will give current timestamp.  
I need the timestamp from the start of the date. (2024-11-15 00:00:00 )

I have this code:  
const moment = require(‘moment’);  
const today = moment();  
var todayDate = today.format(‘YYYYMMDD’);  
**let todayDate = moment(todayDate).unix();**  
pm.globals.set(“todayDate”, todayDate);

This gives me current timestamp.  
Can somebody help me here

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 15, 2024, 10:56am UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270/2 "2024-11-15T10:56:12Z")

</div>

The following appears to work.

```auto
let c = new Date(); // take the current date/time

c.setHours(0);
c.setMinutes(0);
c.setSeconds(0);

const moment = require("moment");

var todayDate = moment(c).format("YYYY-MM-DD HH:mm:ss");

console.log(todayDate); // "2024-11-15 00:00:00"

```

---

<div class="post-metadata">

**Author:** ![technical-geoscien17](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@technical-geoscien17](https://community.postman.com/u/technical-geoscien17)\
**Post date:** [November 18, 2024, 7:42am UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270/3 "2024-11-18T07:42:18Z")

</div>

This does the job.  
Thank you very much.

Another question.  
I was writing a python code to call this api.

I haven’t written a python code with such pre-request script before.  
Can you help me how do we pass this code as a value in python code?  
What are the libraries used?

---

<div class="post-metadata">

**Author:** ![technical-geoscien17](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@technical-geoscien17](https://community.postman.com/u/technical-geoscien17)\
**Post date:** [November 18, 2024, 9:57am UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270/4 "2024-11-18T09:57:49Z")

</div>

> [@technical-geoscien17](#):
>
> const today = moment();

Also,  
**let c = new Date()**  
gives local time. Is there a way to input the GMT time in this statement.

let c = new Date().utc(); → This doesn’t work.

Sorry, i am new to this

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 18, 2024, 10:21am UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270/5 "2024-11-18T10:21:57Z")

</div>

> [@technical-geoscien17](#):
>
> I haven’t written a python code with such pre-request script before.  
> Can you help me how do we pass this code as a value in python code?  
> What are the libraries used?

Postman uses JavaScript under the hood, and moment is a Node.JS JavaScript library.

You will have to find something similar for Python.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 18, 2024, 10:24am UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270/6 "2024-11-18T10:24:31Z")

</div>

> [@technical-geoscien17](#):
>
> gives local time. Is there a way to input the GMT time in this statement.

```auto
let c = new Date().toISOString();

```

[javascript - How to convert a Date to UTC? - Stack Overflow](https://stackoverflow.com/questions/948532/how-to-convert-a-date-to-utc)

---

<div class="post-metadata">

**Author:** ![technical-geoscien17](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@technical-geoscien17](https://community.postman.com/u/technical-geoscien17)\
**Post date:** [November 18, 2024, 11:02am UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270/7 "2024-11-18T11:02:43Z")

</div>

I thought this will work but I would like to refrain my question.

I want to list down only last day’s data(Date to be in UTC).

If now in UTC is 18 Nov 11 AM.  
I should get the date as: 2024 11 17 00:00:00

By using your method, i am getting my local time which when converted to UTC is giving another date and time.

I want last date’s time and date in UTC.  
Thank you for your help.

---

<div class="post-metadata">

**Author:** ![technical-geoscien17](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@technical-geoscien17](https://community.postman.com/u/technical-geoscien17)\
**Post date:** [November 18, 2024, 11:06am UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270/8 "2024-11-18T11:06:12Z")

</div>

const moment = require(‘moment’);

//let c = moment().subtract(1, ‘days’); // take the current date/time

let c = new Date();

console.log(c);

c.setDate(c.getDate()-1);

c.setHours(0);

c.setMinutes(0);

c.setSeconds(0);

console.log(c);

const d= c.toISOString();

console.log(d);  
All these values are coming in IST.  
I want these values in UTC/PST.  
Can you please help.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/f/4faca6e8a64e654c09f2908fb2e271abc65ac57e.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 18, 2024, 1:45pm UTC](https://community.postman.com/t/how-can-i-take-start-of-currentdate/71270/9 "2024-11-18T13:45:13Z")

</div>

The problem is determining the timezone difference.

Therefore I would suggest using the moment timezone functionality.

You can import this into the Sandbox using the following code in a pre-request script.

```auto
if (!pm.globals.has("moment_js") || !pm.globals.has("moment_tz")) {
    pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.45/moment-timezone-with-data-10-year-range.js", (mtzErr, mtzRes) => {
        pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.30.1/moment.min.js", (mjsErr, mjsRes) => {
            pm.globals.set("moment_js", mjsRes.text());
            pm.globals.set("moment_tz", mtzRes.text());
        })
    })
}

(new Function(pm.globals.get("moment_js")))();
(new Function(pm.globals.get("moment_tz")))();

```

[Moment Timezone | Docs](https://momentjs.com/timezone/docs/)

All you need to work out is the date.

Something like…

```auto
let c = moment("2024-11-24T22:00:00Z");

let london = moment.tz(c, "Europe/London").format("YYYY-MM-DD HH:mm:ss"); 
let asiaCalcutta = moment.tz(c, "Asia/Calcutta").format("YYYY-MM-DD HH:mm:ss"); 

console.log(`Asia/Calcultta: ${asiaCalcutta}`); // 2024-11-25 03:30:00
console.log(`London: ${london}`); // 2024-11-24 22:00:00

```

As soon as you format the date into a string, the setHours and setMinutes methods that are available on a date object are no longer available to you.

If you want to zero the hours, minutes and seconds, you can simply do this in the formatting.

```auto
let londonV2 = moment.tz(c, "Europe/London").format("YYYY-MM-DD 00:00:00"); 
console.log(londonV2); // 2024-11-24 00:00:00

```
