Node.js date format conversion without using modules - node.js

Date format conversion without using modules
The following code converts the date into different format: "2016-09-16T04:48:29.250Z"
var date = new Date().toISOString();
console.log(date);
How do I subtract 14 days and then convert the date into the same format?
var date = new Date();
date.setDate(date.getDate()-14);
console.log(date);

If it's already a regular Date object then seems like you pretty much had it already:
var date = new Date();
date.setDate(date.getDate()-14);
console.log(date.toISOString())

You're very close. Just do:
var date = new Date();
date.setDate(date.getDate() - 14);
console.log(date.toISOString());

Modify your code
var date = new Date();
date.setDate(date.getDate()-14);
console.log(date.toISOString())

Related

Reactjs Timezone convertion

I am connected mssql database and get some informations includes Date_Time.
Time is coming like 2021-01-30T15:08:25.357Z. I want to convert it to dd-mm-yy hh:mm:ss format.
So, it should be 30-01-2021 15:08:25.
I used this method but it is not exactly that I want.
var d1 = new Date(datey).toLocaleDateString("tr")
var newTime=d1+" "+
new Date(datey).getUTCHours()+":"+
new Date(datey).getUTCMinutes()+":"+
new Date(datey).getUTCSeconds()
// it returns 30/01/2021 15:8:25
Maybe, In there, I want to see time fomat with 0 such as 15:08. When hour 2 a.m it just 2:0 but I want to see it 02:00.
How should I do , is there any idea?
I would suggest using a date/time library such as moment.js, this will make date manipulation much easier, parsing and formatting your date is then very simple:
const input= "2021-01-30T15:08:25.357Z";
console.log("Input date:", input);
// To convert the date to local before displaying, we can use moment().format()
const formattedDateLocal = moment(input).format("DD-MM-YY HH:mm:ss");
console.log("Formatted date (Local Time):", formattedDateLocal );
// To display the UTC date, we can use moment.utc().format()
const formattedDateUTC = moment.utc(input).format("DD-MM-YY HH:mm:ss");
console.log("Formatted date (UTC):", formattedDateUTC );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Return new date after adding the number to the today's date in node js

I have a logical question may be it sound simple but I am facing problem while solving this. Situation is I have a number of days that is expire days now by using that number I have to create a expire date, let me give you one example.
if today's date is 25-07-2020 and no.of expire days is 10 then expire date should be 04-08-2020
I want to this to implement in node js, right now I am getting today's date using Date method.
const today = new Date().toISOString().replace(/\T.+/, '')
This returns today's date but now I am confused how can I get my expire date after adding no.of days.
You need to use setDate here
var currentDate = new Date();
currentDate.setDate(currentDate.getDate() + 10);
console.log(currentDate) //this will have the new date
You can do this with a function:
function addDays(date, days) {
const copy = new Date(Number(date))
copy.setDate(date.getDate() + days)
return copy
}
const date = new Date();
const newDate = addDays(date, 10);
So, you can't add 10 days by simple manipulation of the Date as a string, since you'd need to account for all sorts of edge cases (change of month, change of year, time-zone changes).
There are lots of good libraries out there that can help you, like date-fns or Moment.js.
However, if you want to do this without a library, it's quite straightforward:
const now = +new Date(); // Gets the date as ms since epoch
const expiry = new Date(now + (10 * 24 * 60 * 60 * 1000)); Adds 10 days worth of ms

Wrong date format with toLocaleString on server side

Using this on a server side (a Firebase function, nodejs )
var d=new Date();
var date = d.toLocaleString('default', { month: 'long'}) + ", " + d.getFullYear();
I am expecting to get March, 2020 , since this is what i get on the JS client side, but instead when run on the server side i get - M03, 2020 .
I tried the following code:
const date = require('date-and-time');
var d = new Date();
var myDate = d.toLocaleString('default', { month: 'long'}) + ", " + d.getFullYear();
console.log(myDate);
Output:
March, 2020
Now, if you are trying to store a Javascript Date object into Firebase, this object will be stored as a Timestamp object. I think this would explain the reason why you are noticing some discrepancies in the format.
Optionally, you can convert the Date object to String and store it in Firebase as such. This is more human-readable than ISO 8601 and Timestamp - 0001-01-01T00:00:00Z
You can read more about this in the following documentation about Timestamp and Data types. Moreover, there exists a Timestamp built-in method toDate() that returns a new Date corresponding to a certain timestamp, if conversion is needed.

Converting Excel date to Moment date gives a wrong year output

I am working on importing data in an Excel file and it has date column.
In my app, that date column value comes as a serial number like 43101.622083333335 which stands for 01/01/2018.
When converting this serial number from Excel back to the normal date it stands for, it gives wrong year.
For example, it gives 01-Jan-1970 instead of 01-Jan-2018
``
var moment = require('moment');
var excelDate = 43101.622083333335;
var date = moment(new Date(excelDate));
var dateWithNewFormat = date.format('DD-MMM-YYYY');
console.log(dateWithNewFormat);
``
Output: 01-Jan-1970 instead of 01-Jan-2018
Any help ?
Thanks in advance.
I don't think this is an issue with the moment library. It seems that you aren't calling Date with a valid constructor argument with new Date(excelDate) (see official documentation for Date here).
The Date class doesn't understand the concept of 'Excel time' but it does understand the concept of a unix timestamp. If you refer to this post, you can see how to convert from Excel time to a unix timestamp, depending on which version of Excel you are using.
Then, I would change your code to:
var moment = require('moment');
var excelDate = 43101.622083333335;
var unixTimestamp = (excelDate-25569)*86400 //as per the post above, convert Excel date to unix timestamp, assuming Mac/Windows Excel 2011 onwards
var date = moment(new Date(unixTimestamp)); //Pass in unix timestamp instead of Excel date
var dateWithNewFormat = date.format('DD-MMM-YYYY');
console.log(dateWithNewFormat);

How to set timezone with moment?

i am using moment for getting server time .
moment.tz.setDefault("Asia/Kolkata");
var now = new Date();
var _p_date = moment.tz(now, zone).format();
time when inserting _p_date = 2016-01-05T18:32:00+05:30
But in database date variable is type of DATETIME. and time is saved as 2016-01-05 18:32:00.
and after that when i comparing with this to get time_ago funcionality. providing me wrong estimation.
using time ago = moment("2016-01-05T18:32:00.000Z").fromNow(); // is showing In 5 hours
Since your initial timezone is lost you have to create moment.tz object with selected timezone. Try this plunker
var date = moment.tz(moment("2016-01-05T18:32:00.000Z", "YYYY-MM-DDTHH:mm")
.format('YYYY-MM-DD HH:mm'), 'Asia/Kolkata');
console.log(date.fromNow());

Resources