Moment local time to utc conversion just giving local time - javascript

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>

Related

How to convert the DD-MM-YYYY HH:mm format to epoch time using moment or js?

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);
}

How to combine date and time into a single datetime object?

In my react native app i need to combine my date and time in to single datetime object. I used react native modal date time picker npm package for get date and time.
I have a datepicker returning a date string, and a timepicker returning a time string. When i try to combine it will give me a output as Invalid date.
concatDateTime = () => {
var date = this.state.date;
var time = this.state.currentTime;
var dateTime = Moment(date + ' ' + time, 'DD/MM/YYYY HH:mm');
console.log(dateTime.format('YYYY-MM-DD HH:mm'));
}
I need dateobject in ('YYYY-MM-DDTHH:mm:s') format.
You can specify the format of your input string to let moment know how to parse it.
var date = '2019-02-16';
var time = '8:24 PM';
// tell moment how to parse the input string
var momentObj = moment(date + time, 'YYYY-MM-DDLT');
// conversion
var dateTime = momentObj.format('YYYY-MM-DDTHH:mm:s');
console.log(dateTime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Just click on below link,
https://stackblitz.com/edit/typescript-bmgx2p?file=index.ts
I hope it will solve your problem.
Another alternative:
let mDate = moment(data.StartDateLocal).tz("Australia/Melbourne");
let mTime = moment(data.StartTimeLocal).tz("Australia/Melbourne");
let x1 = {
'hour': mTime.get('hour'),
'minute': mTime.get('minute'),
'second': mTime.get('second')
}
mDate.set(x1);
this._json.header.transactionStartDateTime = mDate.format("YYYY-MM-DDTHH:mm:ss");

moment timezone returns bad value for unit test

So my project is using moment timezone. And it works perfectly everywhere its used except in a unit test. And I can't figure out why.
I'm calling
moment(new Date(date)).tz(timezone).format(mask);
where the date, mask, and timezone are as follows:
date = "2016-11-11T19:34:56.601Z"
mask = "mm-dd-yyyy"
timezone = "America/New_York"
The result I get is: 34-Fr-yyyy
In another call, the following occurs:
date = "2016-12-13T21:57:53.336Z"
mask = "mm-dd-yyyy"
timezone = "America/New_York"
and the result is 57-Tu-yyyy
You use the wrong date format, the correct is MM-DD-YYYY.
var date = "2016-12-13T21:57:53.336Z";
var mask = "mm-dd-yyyy";
var timezone = "America/New_York";
var dateStr = moment(date).tz(timezone).format('MM-DD-YYYY');
console.log(dateStr);
<script src="http://momentjs.com/downloads/moment.js"></script>
<script src="http://momentjs.com/downloads/moment-timezone-with-data.js"></script>
http://momentjs.com/docs/#/parsing/string-format

how to convert unix time to unix date by moment.js

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.

Convert date format 27.05.2015 01:46:32.UTC to Locale time

Can anyone help me to convert date format 27.05.2015 01:46:32.UTC to Locale time.
I tried the following code
var date = new Date('27.05.2015 01:46:32.UTC ');
date.toString();
This is also not working.
Even momentjs is also gives error to convert this format of date.
As per statement even momentjs is also gives error to convert this format of date, I have taken liberty and used momentjs here.
You need to use string-format moment constructor to pass string and format in which input string is.
Use
var t = "27.05.2015 01:46:32.UTC";
//pass dateTime string and its format, it will return
//moment object
var cdt = moment.utc(t, 'DD.MM.YYYY HH:mm:ss');
//Get date object
var date = cdt.toDate();
//Use date variable as per requiremnt
alert(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
The format you are using for Date is DD.MM.YYYY. So you have to change this to MM.DD.YYYY for a valid java script object.
var date = '27.05.2015 01:46:32.UTC';
var date1 = date.split(' ');
console.log(date1)
var date2 = date1[0].split('.');
console.log(date2)
var date3 = date2[1]+ '.' +date2[0] +'.'+ date2[2];
console.log(date3)
var final_date = date3 + ' ' + date1[1];
console.log(final_date);
final_date.toString();

Categories

Resources