Some string like 1487152419798 its a timestamp with date & time
i need string like 1487152419798 a timestamp with date & 00:00.
let timestamp = '1487152419798';
let date = moment(Number(timestamp)).format('YYYY-MM-DD');
let unixDate = moment.unix(date); //don't work
i need 2017.02.15 00:00 in unix
You can use startOf("day") to modify a Moment instance to set all time fields to 0. So first convert timestamp to a number:
timestamp = Number(timestamp)
and then either:
If you want local:
let local = moment(timestamp).startOf("day");
or if you want UTC:
let utc = moment.utc(timestamp).startOf("day");
// Note---------^^^^^
let timestamp = Number('1487152419798');
let local = moment(timestamp).startOf("day");
let utc = moment.utc(timestamp).startOf("day");
console.log("Date in local timezone: ", local.format());
console.log("Date in UTC: ", utc.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
let timestamp = '1487152419798';
let date = new Date(Number(timestamp));
date should give you the required value.
Related
I have a date in (yyyy-mm-dd) format, i want to get start and end date with timestamps and ISO format, i can concat date and timestamp but i want it in date type not in string,
let date = "2020-09-11";
Expected result should be,
startDate = 2020-09-11T00:00:00.000Z
endDate = 2020-09-11T23:59:59.000Z
The start date is correct, The end date is not correct, it gives different time stamp, 18:29:59.000Z when i set it to setHours(23,59,59),
let startDate = new Date(date);
console.log(startDate);
// output: 2020-09-11T00:00:00.000Z
let endDate = new Date(new Date(date).setHours(23,59,59));
console.log(endDate);
// output: 2020-09-11T18:29:59.000Z
I looked at this question convert string to ISO date time in nodejs, but this does not help me, i don't want to use moment,
Am i doing wrong to convert dates? i am using nodejs.
You should use setUTCHours()
The setUTCHours() method sets the hour for a specified date according to universal time
let date = new Date()
let startDate = new Date(new Date(date).setUTCHours(0,0,0));
let endDate = new Date(new Date(date).setUTCHours(23,59,59));
console.log(startDate);
console.log(endDate);
You could just add the postfix without conversion to a local date.
let date = "2020-09-11",
startDate = date + 'T00:00:00.000Z',
endDate = date + 'T23:59:59.000Z';
console.log(startDate);
console.log(endDate);
I have a date time picker and it omits date in format of a DD-MM-YYYY HH:mm a format. I want to convert it into epoch time to store it in the database backend.
I have tried using the following methods so far, but all of them return NaN as output:
var loadingPlannedUnTime = this.state.loadingPlannedUnTime;
console.log("normal conversion"+loadingPlannedUnTime);
//returns 9-08-2020 15:08:00
//Moment method
var loadingPlannedUnTime = moment(this.state.loadingPlannedUnTime).unix();
console.log("moemnt"+loadingPlannedUnTime)
//Returns NaN
//JS getTime() method
var pickupDateTime = new Date(this.state.loadingPickupTime);
var loadingPickupTime = pickupDateTime.getTime();
console.log("new date and gettime"+loadingPickupTime)
//Returns NaN
What is the correct method to convert it to epoch time?
https://momentjs.com/docs/
If you know the format of an input string, you can use that to parse a moment.
moment("12-25-1995", "MM-DD-YYYY");
const x = moment('24-12-2019 09:15', "DD-MM-YYYY hh:mm");
console.log(x.format())
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
const matches = loadingPlannedUnTime.match(/(\d{1,2})-(\d{1,2})-(\d{2,4}) (\d{1,2}):(\d{1,2}):(\d{1,2})/);
if (!!matches) {
// new Date(year, month, day, hours, minutes, seconds, milliseconds)
const epoch = new Date(matches[3], matches[2] - 1, matches[1], matches[4], matches[5], matches[6]).getTime();
console.log(epoch);
}
My Incoming Date is in format : 15.08.2017 23:03:23.120000
Here I am using Node-Red Platform to convert msg.payload.time in Influx timestamp but I am getting this Error:
"Error: Expected numeric value for, timestamp, but got '15.08.2017 23:03:23.120000'!".
Please let me know the script for given timestamp to influxdb timestamp.
InfluxDB expects unix timestamps and msg.payload.time might be a string, hence you are getting the error.
In order to generate a timeStamp from a date, you can use the Date functionality of JS.
It works in the following way:
new Date('<your-date-string>').valueOf()
Here the date-string is expected in 'YYYY-MM-DD hh:mm:ssZ' format.
In your case, since the msg.payload.time is available in dd.mm.yy hh:mm:ssZ format, you will need to perform some additional operations.
You can update your code as below:
const incomingDate = msg.payload.time;
// extract the date dd.mm.yyyy from the incoming Date String
const splittedDate = incomingDate.split(' ');
// Convert the date from dd.mm.yyyy to yyyy-mm-dd format
let date = splittedDate[0].split('.').reverse().join('-');
// Store time value in a separate variable for later use.
const time = splittedDate[1];
// merge date and time to form yyyy-mm-dd hh:mm:ssZ format
const datetime = `${date} ${time}`
// assign the timestamp value to fields.time
fields.time = new Date(datetime).valueOf();
Here is a working example
const incomingDate = '15.08.2017 23:03:23.120000';
const splittedDate = incomingDate.split(' ');
let date = splittedDate[0].split('.').reverse().join('-');
const time = splittedDate[1];
const datetime = `${date} ${time}`
console.log(datetime);
console.log(new Date(datetime).valueOf())
I am trying to convert string to time, the string i have are in this format, '8:3' and '16:45'.
I want to convert UTC time in jQuery.
You can write your function to create UTC date with the time string.
function toUTC(str) {
let [h, m] = str.split(':');
let date = new Date();
date.setHours(h, m, 0)
return date.toUTCString();
}
console.log(toUTC('8:3'))
console.log(toUTC('16:45'))
You don't need jQuery for such operations. Just the simple Date object will do the trick. Say you want to convert time from a specific date.
let date = new Date('2020-04-01'); // leave the Date parameter blank if today
date.setHours(16); // must be 24 hours format
date.setMinutes(45);
let theUTCFormat = date.getUTCDate();
Cheers,
I'm trying to get a UTC time string in YYYY-MM-DD HH:MM:SS format but when using moment it's just giving the local input time back, what am I doing wrong?
moment('2019-04-16 22:00:00:').utc()
This returns a moment object with the value of 2019-04-16 22:00:00
This is a working example which converts local time to UTC. Check it out
var local = moment.utc().local().format("YYYY-MM-DD HH:mm:ss");
console.log(local, "- local");
var date = moment(local).utc().format("YYYY-MM-DD HH:mm:ss");
console.log(date, "- local now in UTC");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
You can use moment-timezone library, you can input any timezone want to convert to UTC.
var input = "2019-04-16 22:00:00"
var format = "YYYY-MM-DD HH:mm:ss";
var yourzone = "Asia/Seoul";
var yourtime = moment.tz(input, format, yourzone);
// convert to utc
yourtime.utc();
// format output
var result = yourtime.format(format)
console.log(result)
var input = "2019-04-16 22:00:00"
var format = "YYYY-MM-DD HH:mm:ss";
var yourzone = "Asia/Seoul";
var yourtime = moment.tz(input, format, yourzone);
// convert to utc
yourtime.utc();
// format output
var result = yourtime.format(format)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone-with-data-2010-2020.min.js"></script>
<p id="london"></p>