This is the format of date saved in MongoDB:
Sun Feb 13 2022 05:30:00 GMT+0530 (India Standard Time)
How to separate month, year and day from this date in pug? I want to take this date in pug and display month and year separately.
Pug is built on JavaScript, so you could just use string manipulation to parse the month, day, and year—assuming the format is consistent.
-
// variable containing date string
const myString = "Sun Feb 13 2022 05:30:00 GMT+0530 (India Standard Time)"
// break into individual variables using destructuring
let [dayOfWeek, month, day, year, time, timeZone, timeZoneName] = myString.split(' ')
p.postDate Posted on #{month} #{day}, #{year}
Thanks everyone for your help, i found a npm library called moment.js. It is super easy to use and also has many built in functions.
Related
Is there any way to get a localized format in DayJS plugin but without the day? I need to show in a calendar the month and year only but depending on the locale, the month can be on the first place or at the end.
I've checked in the docs (https://day.js.org/docs/en/display/format#localized-formats) but it seems there is nothing with year and month only.
dayjs().format("LL") // December 7, 2022
// what I would like to get: December 2022 or 2022 December (depending on the locale)
Thanks in advance
Date time appears as "Tue Nov 09 2021 20:27:51 GMT-0500 (Eastern Standard Time)
How to convert to a value that I can use to calculate time difference between two dates?
Do you want to show the difference in time(hours) or days?
For days you can use a column with the following formula
=CONCAT(MID(A3,9,2),"-",MID(A3,5,3),"-",MID(A3,12,4))
where A3 is your cell containing the string "Tue Nov 09 2021 20:27:51 GMT-0500 (Eastern Standard Time)". This will be converted to 09-Nov-2021. You can then use a normal subtraction (A-B) to calculate the day difference between 2 dates.
Use MID:
=MID(REPLACE(A1,11,0,","),5,21)-TIME(5,0,0)
Then format it how you would like.
I've been searching all over and just can't find a explanation or reason why this is happening but the parse(String) method of DateFormat just isn't parsing my String correctly.
I'm trying to parse a String into the date format that is used for HTTP headers and got as far as getting the String on its own such as:
Thu, 11 Nov 2010 18:34:22 GMT
Which is in the format:
E, d MMM yyyy HH:mm:ss z
But when I use df.parse(dateStr); this is what I get out of it:
Thu Nov 11 18:34:22 GMT 2010
Which is nothing like what I wanted, why is the year now after the GMT? Why is there no comma anymore? And why is the date after the month?
I'm completely confused about this now and can't find a solution but I really need the date to be in that format. Is the comma messing things up? or the colons?
Thanks for your time,
Infinitifizz
P.S.
Forgot to mention this but I've tried dateformat.setLenient(false) and it makes no difference.
P.P.S
I'm trying to do this to compare the dates with date1.before(date2) and after() etc to see if one is newer than the other but I can't do this because the parsing isn't working.
Even though they look the same but just the format is different, they are not the same because after calling getTime() on both of them (When I have provided 2 identical dates) the longs are not the same. As in the date is:
Thu, 11 Nov 2010 19:38:52 GMT for a lastModified() on a File
If I input the String "Thu, 11 Nov 2010 19:38:52 GMT" and then compare their longs once converting the string to a date using parse() and then calling getTime() on that date I get:
lastModified = 1289504332671
fromString = 1289504332000
It is only the last 3 digits that are different, does this have any significance?
Thanks again for your time and sorry I didn't put this bit in first,
Infinitifizz
The result format is the default format of Date#toString() (click link to see the javadoc). You're apparently doing a System.out.println(date). You would like to use SimpleDateFormat#format() instead with another pattern to format it in the desired format. E.g.
String newDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
Update: You shouldn't care about the format when using Date. You should only care about the format at that point when Date is to be converted (displayed) as String. As to the difference in timestamps, the Date uses millisecond precision for the timestamp while HTTP header uses second precision. You'd like to divide the timestamps by 1000 before comparing.
I have a list of datetimes that happen to have the ordinal in them (1st, 2nd, 3rd). This causes problems for working with dates and converting to unix time.
How to remove the 'rd' or 'th' suffixes from dates?
example dates:
April 23rd
Apr 24th
Apr 30th
May 1st
May 7th
May 8th
May 15th
May 21st
May 22nd
May 28th
Jun 18th
Jun 19th
Jun 26th
Try this:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"st",""),"nd",""),"rd",""),"th","")
and, to convert it to a "real" date with today's year:
=--SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"st",""),"nd",""),"rd",""),"th","")
or, shorter (and works in Excel, not sure about Google):
=LOOKUP(1E+307,--SUBSTITUTE(A1,{"st","nd","rd","th"},""))
Be sure to format it as a Date
If the dates were just text and always had the last two letters, then it would just be
=LEFT(A1,LEN(A1)-2)
If you wanted to check that the letters were there first, you could use something like
=IF(OR(RIGHT(A1,2)={"st","nd","rd","th"}),LEFT(A1,LEN(A1)-2),A1)
Has to be entered as an array formula in Google Sheets
=ArrayFormula(=IF(OR(RIGHT(A1,2)={"st","nd","rd","th"}),LEFT(A1,LEN(A1)-2),A1))
I've got a date string from my database which has this format:
Tue Nov 12 2013 18:14:46 GMT+0100 (CET)
I want to convert it into a ISOString and im currently doing that with:
var iso = new Date(val.text_date).toISOString();
However for some reason the output time is moved 1 hour backwards?
This is the output im getting:
2013-11-12T17:14:46.000Z
How can i avoid this?
Short answer: the time is converted into UTC, and your original time was displayed in UTC+1, hence the one hour difference.
The Date.toISOString() method converts the date into a string in the ISO 8601 format. Note that the returned date in your example ends by a Z: 2013-11-12T17:14:46.000Z. As per the Mozilla documentation and Wikipedia:
If the time is in UTC, add a Z directly after the time without a
space. Z is the zone designator for the zero UTC offset