Moment.js conversion of RFC2822 date - javascript

I have a date as '04 FEB 1994' which I'm trying to convert into a date object for comparison.
I've tried a whole bunch of variations, the current one is...
var trydate = moment(licDob, "DD-MMM-YYYY");
var momentObj = moment(trydate);
var momentString = momentObj.format('YYYY-MM-DD');
where licDob is 04 FEB 1994. momentObj is coming back as 853506000000 which makes momentString 1997-01-18.
Guidance much appreciated.

use
var newTry = moment(licDob,'DD MMM YYYY').toDate()
considering you want to convert it to Date()

You were close the the right solution. You have to use the moment constructor with 2 parameters. The 1st parameter is your string date and the 2nd is the format.
In your case the correct format is DD MMM YYYY
const stringDate = "04 FEB 1994";
const momentDate = moment(stringDate, "DD MMM YYYY");
console.log("Formatted:", momentDate.format("DD/MM/YYYY"));
console.log("EPOCH:", momentDate.valueOf())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Related

Format Date based on Browser time zone (VueJs/JS)

I have a Date in string Format: 2020-07-13 7:07 AM (which is indian time). I need to change this time based on browser time zone which can be either in US or Africa.
I have tried following ways, but i am not able to convert it correctly.
Attaching my steps:
var d = "2020-07-13 7:07 AM";
var date = new Date(d); //Mon Jul 13 2020 07:07:00 GMT+0530 (India StandardTime)
var date1 = moment(date, 'YYYY-MM-DD HH:mm T'); //undefined
Please help me out. I havee to this in both VueJs and Javascript
You should convert your timestamps to an ISO 8601 format including the UTC-offset. This can be done for instance using new Date().toISOString(). Then you can feed the timestamp into moment or, if you want to display the time for a different timezone, have a look at moment-timezone
I figure out the answer in this way:
var buildDate = "2020-07-13_7:07";
let releaseDate = buildDate .split('_');
let date = moment(date);
let newDate = moment(new Date(date.get('year'), date.get('month'), date.get('date'), date.get('hour'), date.get('minute'))).add(new Date().getTimezoneOffset()*-1, 'm').toDate();
let result = moment(newDate).format('MMM DD YYYY h:mm A');

How to grab standard date from DateTime object in Javascript

My date is as followed 2020-05-13T18:00:00+01:00
I am trying to grab the time, but in standard format. In this case, it'll be 06:00:00 pm
May I have some guidance on how to achieve this?
I've tried the following.
vm.training.startDate = '2020-05-13T18:00:00+01:00`'
var start = new Date(vm.training.startDate)
var locale = start.toLocaleTimeString(); // Produced 1:00:00 PM (not the value I'm looking for)
var test = start.getTime(); // produced 1589389200000
var test1 = start.toTimeString(); // produced 13:00:00 GMT-0400 (Eastern Daylight Time)
var test3 = moment(vm.training.startDate, 'MM-DD-YYYY hh:mm A') // moment date object
This one is quite close but still slightly off
var test4 = moment(vm.training.endDate, 'HH:mm').format('hh:mm:ss a')// produced `08:30:00 pm`
Any tips would be greatly appreciated
Use momentjs format to convert a date string to the respective format by specifying the format string.
Docs: https://momentjs.com/docs/#/parsing/string-formats/
console.log(
moment("2020-05-13T18:00:00+01:00", "YYYY-MM-YYYY HH:mm").format("hh:mm:ss a")
);
<script src="https://momentjs.com/downloads/moment.js"></script>
You can pass the timezone option when calling toLocaleTimeString() method:
var options = { timeZone: 'Europe/London'};
var d = new Date('2020-05-13T18:00:00+01:00');
var n = d.toLocaleTimeString('en-UK', options);
console.log(n)

Format the current date and time using momentjs

I want to convert the current date and time to in the following way using moment.js.
Current date and time using javascript new Date(): Thu Jul 12 2018 09:28:51 GMT+0530 (India Standard Time) and want to convert the above format to below mentioned format.
1. Thu, 12 Jul 2018 09:31:37 GMT
2. 2018-07-12T09:31:38Z
You can learn more about formatting with moment.js here.
Escape words in formatting with escaping-characters "[]".
console.log(moment());
console.log(moment().format('ddd, DD MMM YYYY HH:mm:ss [GMT]'));
console.log(moment().format('YYYY-MM-DD[T]HH:mm:ss[Z]'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
you can also try like below for
nodejs
var moment = require('moment');
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();
console.log(format1);
console.log(format2);
angular
import moment from 'moment';
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();
console.log(format1);
console.log(format2);
Install moment like below
npm install --save moment
Html javascript
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();
console.log(format1);
console.log(format2);
moment source script
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
You can use toGMTSting method to convert local time into GMT format. Hope this helps..
console.log(new Date().toGMTString());
console.log(new Date("Fri Jan 20 2012 11:51:36 GMT-0530").toGMTString());
You can try these also:
var moment = require('moment');
let startDate= new moment('11/22/1990','MM/DD/YYYY').format("YYYY/MM/DD");
You can also understand these:
let otherDate= 1399919400000;
var neweDate = moment(otherDate).format('DD/MM/YYYY');
//My neweDate output is "13/05/2014";
moment.locale('cs');
console.log(moment.locale()); // en
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
You can verify the date also:
moment("not a real date").isValid(); // false
moment("2010 13", "YYYY MM").isValid(); // false (not a real month)
moment("2010 11 31", "YYYY MM DD").isValid(); // false (not a real day)
moment("2010 2 29", "YYYY MM DD").isValid(); // false (not a leap year)
moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)
You can create a Moment with a pre-existing native Javascript Date object.
var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);
You can create a moment with an array of numbers that mirror the parameters passed to new Date()
[year, month, day, hour, minute, second, millisecond]
moment([2010, 1, 14, 15, 25, 50, 125]); // February 14th, 3:25:50.125 PM
Any value past the year is optional, and will default to the lowest possible number.
moment([2010]); // January 1st
moment([2010, 6]); // July 1st
moment([2010, 6, 10]); // July 10th

Use Moment.js to convert Unix epoch time to human readable time

I'm trying to use Moment.js to convert a Unix epoch time to a date and time. I'd also like to know how to have it formatted like below.
Tuesday, November 22, 2016 6:00 PM
moment.unix(yourUnixEpochTime).format('dddd, MMMM Do, YYYY h:mm:ss A')
From the Docs: Unix Timestamp
var day = moment.unix(1318781876); //seconds
var day = moment(1318781876406); //milliseconds
// and then:
console.log(day.format('dddd MMMM Do YYYY, h:mm:ss a'));
// "Sunday October 16th 2011, 9:17:56 am"
You can use .format('LLLL') for your requirement.
let result = moment(epoch).format('LLLL');
let epoch = 1562127342123;
let result = moment(epoch).format('LLLL');
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.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
You can use moment.unix(epochTime).
Moment JS converts the Unix time to GMT equivalent. To convert it to EST which is 5 hours behind Coordinated Universal Time (UTC) :
[momentjs]
const moment = require('moment');
console.log(moment(1580331903396).subtract(5, 'hours').format('MMMM Do, YYYY - h:mm:ss A '))
Refer working example here:
https://repl.it/repls/CumbersomeImmediateLibrary

UTC date convert to local timezone

I have a date in UTC format.
"2016-10-12 05:03:51"
I made a function to convert UTC date to my local time.
function FormatDate(date)
{
var arr = date.split(/[- :T]/), // from your example var date = "2012-11-14T06:57:36+0000";
date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], 00);
var newDate = new Date(date.getTime()+date.getTimezoneOffset()*60*1000);
var offset = date.getTimezoneOffset() / 60;
var hours = date.getHours();
newDate.setHours(hours - offset);
return newDate;
}
My Local timezone is GMT +0530.
My code produced this output:
Tue Oct 11 2016 10:33:00 GMT+0530 (IST)
I converted the date with an online tool to get the correct date and time.
Wednesday, October 12, 2016 10:30 AM
My code matches the online tool on time but not on date.
How can I correct my code's output, preferably using moment.js?
UTC is a standard, not a format. I assume you mean your strings use a zero offset, i.e. "2016-10-12 05:03:51" is "2016-10-12 05:03:51+0000"
You are on the right track when parsing the string, but you can use UTC methods to to stop the host from adjusting the values for the system offset when creating the date.
function parseDateUTC(s){
var arr = s.split(/\D/);
return new Date(Date.UTC(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]));
}
console.log(parseDateUTC('2016-10-12 05:03:51').toLocaleString());
If you want to use moment.js, you can do something like the following. It forces moment to use UTC when parsing the string, then local to write it to output:
var d = moment.utc('2016-10-12 05:03:51','YYYY-MM-DD HH:mm:ss');
console.log(d.local().format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.0/moment.js"></script>
Since you have tagged moment, I'm assuming you are using moment.
In such cases, you should keep your approach consistent and not mix moment and date object.
var dateStr = '2016-10-12 05:03:51';
var timeZone = "+0530";
var date = moment.utc(dateStr).utcOffset(dateStr + timeZone)
console.log(date.toString())

Categories

Resources